2

I have an Activity in which I have few fragments.

Fragments are all set to false for retain instance property which means that they are destroyed when screen oritenation changes. (Plus, they are not in back stack)

When I change the screen orientation, I observed that fragments are destroyed and recreated which is expected.

What I don't expect is - re-attaching those fragments by the activity during activity re-creation. I have a null check inside Activity's onCreate method which determines if fragment is exists or not and according to this decision it's added or attached.

Now, if all fragments are deleted/destroyed, how fragment is attached in onCreate of activity. Shouldn't be added since it's destroyed?

I suspect that super.onCreate(savedInstanceState) of Activity's onCreate loads the fragment again here.

Any ideas?

stdout
  • 2,471
  • 2
  • 31
  • 40
  • Have you save the state of Fragments?? – Piyush Aug 14 '13 at 08:31
  • Unless you post code for your Activity it is difficult to tell what is happening just from your description. – Squonk Aug 14 '13 at 08:37
  • I don't explicitly save their states but as I mentioned, system may implicitly save the state of the activity(and thus it's fragments) in onSaveInstance..() method just before it kills the activity and reload during re-creation. – stdout Aug 14 '13 at 08:40
  • Code is really hard to put in a simple snippet. By the way I found a thread regarding my issue; http://stackoverflow.com/questions/8474104/android-fragment-lifecycle-over-orientation-changes – stdout Aug 14 '13 at 08:41

1 Answers1

0

This appears to be a duplicate of: Android Fragment lifecycle over orientation changes

Short answers from above: "When a config change occurs the old Fragment isn't destroyed -- it adds itself back to the Activity when it's recreated. This is a massive pain in the rear most of the time."

"to ensure a consistent user experience, Android persists the Fragment layout and associated back stack when an Activity is restarted due to a configuration change." (p. 124 of Reto Meier book)

Don't call the super.onSaveInstanceState() in the method: This would most likely break the activities lifecycle introducing more potential problems in this already quite messy process. Look into the source code of FragmentActivity: it is saving the states of all fragments there. – Brian Jan 15 '13 at 10:08

Community
  • 1
  • 1
Haluk
  • 11
  • 3