In OnCreate when the application starts, fragment1 is called with TextView1 with text. There is a button that switches to another fragment - fragment2 with TextView2 and text in it. So, if you click on the button , it switches to fragment2 . But if you turn the smartphone over, it switches to horizontal orientation and on top of TextView2 , as if in the background, TextView1 opens. If you return back, then even more layered.

As I understand it, when changing the screen orientation, Android calls OnCreate again. How to ban it? But at the same time, so that nothing changes except orientation. That is, if I clicked on the button and read TextView2 , so that when I change the orientation, I stay in the same fragment with the same TextView2 ?

Registered in the Manifesto:

 android:configChanges="keyboardHidden|orientation|screenSize" 

But it did not help. Perhaps you need to add something else, or it is completely wrong. Please tell me how to solve this problem.

  • the onCreate call in any case will be after the change of orientation. You just need to do a check in OnCreate. Read towards savedinstance. - Nikotin N
  • If you want to be helped, attach the problem code. I can assume that you add fragments by the add() method, so layering occurs. Add them with the replace() method and save the state when the configuration changes so that you can restore it later. - pavlofff
  • It seemed to me that I understood clearly. It's not about the fragments. I add using the replace () method. Everything works fine. The problem is in orientation. - Alexey
  • @Alexey, when you turn the screen, the activation will be recreated in any case, you just need to save the state and process it when recreating. There is a lot of information on the Internet, try searching (for example, "saving fragments when you rotate the screen") before asking a question. - Nikotin N
  • Thank. Problem solved. Everything is much simpler) - Alexey

1 answer 1

Problem solved. Here is the solution for anyone facing the same problem. Hope to help you too.

I added a line to AndroidManifest.xml

 android:configChanges="orientation|screenSize" 

It needs to be added to <activity> !

It turns out like this:

 <activity android:name=".MainActivity" android:configChanges="orientation|screenSize">