0

So I have an Activity showing a Fragment (lets call A) which also shows a DialogFragment (lets call B). While "B" is open the user rotates and then dismisses "B". The result is "A" does not know any of it's variables anymore ... The main fragment "A" seems to run OnCreateView when a rotation occurs but not when "B" is displayed.

I have put breakpoints on the constructor, OnCreateView, OnResume and OnStart of fragment "A". When there is a dialog shown the only thing that happens during a rotate for the Activity's Fragment is the constructor. But when there is no DialogFragment in memory during rotation the life-cycle events, most importantly OnCreateView, occur as you would expect.

Here is the code that happens in the Activity during the orientation change from OnCreate (Bundle bundle):

string fTag = "ftag";
//will work, but shows null if dialog was open during rotate
_fragment = (CustomFragment) FragmentManager.FindFragmentByTag(fTag);
if(_fragment == null) {
  _fragment = new CustomFragment(fTag, "Title Here");
}

In the chunk of code above, the FragmentManager finds the Fragment if there is no dialog but does not find the Fragment when the dialog is displayed after an orientation change ...

Has anyone encountered this? Am I missing a possible life-cycle event that would work? I don't get the thinking of the Android SDK developers at all.

Paul Hart
  • 11
  • 3
  • question is how did you add the fragment(A) to an activity ... by code? or from layout ... if by code, are you creating new instance of A, every time, inside of onCreate of Activity or check if `savedInstanceState != null` and ask the FragmentManager for it's prev instance? – Selvin Jan 20 '15 at 16:47
  • Selvin, I have added the code that gets called in the Activity OnCrateView. And you are right, it is creating a new instance of the Fragment. However, just confirmed that the FindFragmentByTag is null only when a dialog is present. Otherwise this function finds the old fragment by the string id. – Paul Hart Jan 20 '15 at 16:54
  • I just read in another post that a fragments "id" becomes a combination of the string I provide as well as a combination of the activity's id? So the restore IDs must now all be stored somehow with this wacky recreation process Android has be so gracious to spam all of the world ... – Paul Hart Jan 20 '15 at 17:31

0 Answers0