I am trying to prevent dialogs built with Alert builder from being dismissed when the Activity is restarted.
If I overload the onConfigurationChanged method I can successfully do this and reset the layout to correct orientation but I lose sticky text feature of edittext. So in solving the dialog problem I have created this edittext problem.
If I save the strings from the edittext and reassign them in the onCofiguration change they still seem to default to initial value not what was entered before rotation. Even if I force an invalidate does seem to update them.
I really need to solve either the dialog problem or the edittext problem.
Thanks for the help.
I had a similar problem: when the screen orientation changed, the dialog's
onDismiss
listener was called even though the user didn't dismiss the dialog. I was able to work around this by instead using theonCancel
listener, which triggered both when the user pressed the back button and when the user touched outside of the dialog.You can combine the Dialog's onSave/onRestore methods with the Activity's onSave/onRestore methods to keep the state of the Dialog.
Note: This method works for those "simple" Dialogs, such as displaying an alert message. It won't reproduce the contents of a WebView embedded in a Dialog. If you really want to prevent a complex dialog from dismissal during rotation, try Chung IW's method.
It seems that this is still an issue, even when "doing everything right" and using
DialogFragment
etc.There is a thread on Google Issue Tracker which claims that it is due to an old dismiss message being left in the message queue. The provided workaround is quite simple:
Incredible that this is still needed 7 years after that issue was first reported.
If you're changing the layout on orientation change I wouldn't put
android:configChanges="orientation"
in your manifest because you're recreating the views anyway.Save the current state of your activity (like text entered, shown dialog, data displayed etc.) using these methods:
That way the activity goes through onCreate again and afterwards calls the onRestoreInstanceState method where you can set your EditText value again.
If you want to store more complex Objects you can use
Here you can store any object and in onCreate you just have to call
getLastNonConfigurationInstance();
to get the Object.Definitely, the best approach is by using DialogFragment.
Here is mine solution of wrapper class that helps to prevent different dialogs from being dismissed within one Fragment (or Activity with small refactoring). Also, it helps to avoid massive code refactoring if for some reasons there are a lot of
AlertDialogs
scattered among the code with slight differences between them in terms of actions, appearance or something else.When it comes to Activity you can invoke
getContext()
insideonCreateDialog()
, cast it to theDialogProvider
interface and request a specific dialog bymDialogId
. All logic to dealing with a target fragment should be deleted.Usage from fragment:
You can read the complete article on my blog How to prevent Dialog being dismissed? and play with the source code.