Answer the question
In order to leave comments, you need to log in
How to check if an activity has active fragments?
Good day.
When starting in my application, a fragment is added in the onCreate method. This fragment, as it were, is the starting one for the application (the initial window).
When the screen is rotated, this fragment is added to the new activity, and another fragment is created in the onCreate method, which is superimposed on the transferred one.
How to check if there are fragments on an activity?
And about findFragmentByTag or findFragmentById. I will have several fragments, maybe even a lot, and I would not want to check for the presence of all of them on the activity. Therefore, I ask you to suggest a verification method that does not require specifying specific activities. Or tell me how to process the change of screen orientation in a different way, so that such problems with the overlay of the activity would not occur.
In general, please help))
Answer the question
In order to leave comments, you need to log in
Use replace() instead of the add(Fragment) method. And you can also check for null of the savedInstance parameter. If savedInstance == null, then do replace. This will only happen the first time the Activity is created. On rotation, savedInstance will not be null and the Fragment will not even be re-added.
Fragment checks are not needed in this case.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question