Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.Start your free trial
Brad Wilder3,541 Points
onCreate method relaunching DialogFragment on device rotation
In my app I have developed a Welcome DialogFragment that it's only purpose is to welcome them to the app, give a brief explanation on how the app works, and then the user can close it. If they press the confirmation button I have a key-value pair that is adjusted and it is never shown again. If they exit from it by other means (i.e. pressing the back button or tapping outside of the DialogFragment) then it doesn't adjust the pair so the user can see it again at launch. I have however ran into a problem with this in that when the user rotates their device the activity's onCreate method gets called again and if the pair isn't set right it will relaunch my DialogFragment. I have looked for a solution to this issue but haven't found one.
I have figured out a work around by having this DialogFragment pop-up when the user clicks on Help from the app's menu. I'd also have a pop-up or toast notify the user they can find it there if they need it again once they click the confirm button. I would also have to prevent exiting this by any other means. Although this would solve this problem it seems like there should be some way of preventing this without altering the design plans for the app.
Ideally I would like to be able to force the application to stick to portrait orientation but haven't been able to do that. I have managed to do that with the activities by adding android:screenOrientation="portrait" to their activity tag in the manifest but the DialogFragments I use still get recreated in landscape once the device is rotated. I feel like this alone might solve the problem but I can't find anything for it.
To sum up what I am wanting I would like it to either: A) Stop recreating the DialogFragment just because the user rotates their phone or B) Have the user rotating their phone have no effect on the app at all.
Thanks for any and all help with this.