2

Most of the code is taken from Android's documentation on Fragments & DialogFragments directly.

I have an activity, with a reference to a Fragment. This fragment creates and shows a DialogFragment, which takes in an edit text and calls back on submit to the Activity (via listener); the activity then updates a label on the original fragment.

This works as expected on initial launch; the fragment's label is changed as expected. However after an orientation change, the original fragment's getView() or getActivity() are both null; you are unable to change the label after that orientation change.

Android Studio project showing this issue; feel free to check it out and give it a run: https://github.com/werelord/testFragment

For quick browsing:

Steps to recreate the issue:

  1. Launch app
  2. Click button to show the Dialog; set the text, click ok: Label is updated correctly
  3. Rotate the display to either Landscape, or Landscape then back to Portrait
  4. Click the button to show the Dialog; set the text; Label is not updated due to getView() being null.

Note that I am not trying to have the dialog fragment handle orientation changes; the orientation change happens before the dialog is created. The activity and fragment are recreated just fine on the orientation change; the alert dialog, when created, is getting the correct reference to the correct activity (recreated one after orientation change), and the referenced fragment within that activity is the correct reference; just that all its views are null.

One solution that does work is obviously setting the android config changes ("orientation|screenSize|keyboardHidden"), but I want to avoid that solution if possible.

I find it strange that it works fine on initial app, which makes me believe that a reference somewhere was destroyed and recreated; but through all my debugging, the MainActivity and TestFragment references are correct (the recreated references).

I've looked through various Q's on SO here, but haven't found anything that fixes the issue I'm seeing. What am I missing here?

George Mulligan
  • 11,813
  • 6
  • 37
  • 50
cyrix
  • 308
  • 2
  • 7
  • Are you planning on adding additional fragments? Is that why you used a `ViewPager` with a `FragmentStatePagerAdapter`? – George Mulligan Feb 17 '16 at 21:49
  • Yeah, more fragments are used; I just limited it to just one in the example to make things simple. – cyrix Feb 18 '16 at 17:56

1 Answers1

2

The problem is that after a rotation change the FragmentStatePagerAdapter is internally recreating the TestFragment for you. the getItem(...) method of the SectionsPagerAdapter is not called.

This means that the Fragment you are referencing in the testFragment variable is not actually the one being displayed. It is never attached to an activity and its layout is never inflated so calling setLabelText on it will throw a NullPointerException.

To fix this you can stop using a ViewPager altogether since you only have one Fragment. Then you can handle adding the Fragment and looking it up properly by id or tag after an orientation change using the FragmentManager.

If you are planning on eventually using more fragments then you will have to use one of the workarounds from this post to get the current fragment out of the SectionsPagerAdapter after an orientation change.

Community
  • 1
  • 1
George Mulligan
  • 11,813
  • 6
  • 37
  • 50
  • This is precisely it; I had no idea FragmentManager was handling recreating those fragments. I do have more than one fragment; I just limited it to just one for the example. Thanks! – cyrix Feb 18 '16 at 17:55