5

I am trying to figure out how onSaveInstanceState/onRestoreInstanceState work with a dialog. With an Acitivity it is easy since they are invoked when the Activity is killed/restarted.

Our Activity displays a login dialog. When is the onSaveInstanceState/onRestoreInstanceStateof the dialog called?

Does it get called automatically when we unbundle the object?

kord
  • 482
  • 3
  • 12
theblitz
  • 6,683
  • 16
  • 60
  • 114
  • See https://stackoverflow.com/questions/7557265/prevent-dialog-dismissal-on-screen-rotation-in-android for using fragments instead for manging dialogues. – Elliott Beach Aug 10 '17 at 18:36

3 Answers3

1

@theblitz: Yes, it is somewhat inconvenient to manage the lifecycle of a Dialog from an Activity.

I had getter methods in the Dialog to retrieve its state variables and I then store these in the Activity's Bundle. Upon onResume or onCreate of the Activity, I retrieve the stored variables from the Bundle, and pass them to the Dialog's parameterized constructor to create a new Dialog. So now I have a Dialog that gives the illusion of being innately state-maintained.

Arun PB
  • 229
  • 2
  • 4
1

The documents in Dialog say:

Note: Activities provide a facility to manage the creation, saving and restoring of dialogs. See {@link Activity#onCreateDialog(int)}, {@link Activity#onPrepareDialog(int, Dialog)}, {@link Activity#showDialog(int)}, and {@link Activity#dismissDialog(int)}.

So if you want to know When is the onSaveInstanceState/onRestoreInstanceState of the dialog called? You should create your dialog in Activity#onCreateDialog(int) and show you dialog Activity#showDialog(int). You can see Activity#performSaveInstanceState , it will call Activity#saveManagedDialogs. But you should use DialogFragment now.

Egos Zhang
  • 1,334
  • 10
  • 18
0

Dialogs shouldn't be used or expected to work like regular activities. They should be used to gather user input or display information. You can create custom layouts for them using UI elements such as checkboxes, textviews etc and capture the user information and store it for later usage.

In your login dialog, get the user data by creating a custom layout with textviews for username, password etc, Store this info in your app and later use it however you like.

Here's a excellent guide for the official docs on how to properly use dialogs:

http://developer.android.com/guide/topics/ui/dialogs.html

Anup Cowkur
  • 20,443
  • 6
  • 51
  • 84
  • But why then does the Dialog class includes onSaveInstanceState/onRestoreInstanceState methods? – theblitz Oct 11 '12 at 06:26
  • Well sometimes, for special requirements, you might need to subclass the dialog class and manage lifecycles. But the method I described above is the recommended method by Google according to their developer guide. – Anup Cowkur Oct 11 '12 at 06:37
  • 1
    Problem is - what to do if the Activity is killed by Android and then restarted. If the Dialog is currently displayed then it does not reappear by itself. – theblitz Oct 11 '12 at 07:43