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:
- Launch app
- Click button to show the Dialog; set the text, click ok: Label is updated correctly
- Rotate the display to either Landscape, or Landscape then back to Portrait
- 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?