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?
The onSaveInstanceState() method allows you to add key/value pairs to the outState of the app. Then the onRestoreInstanceState() method will allow you to retrieve the value and set it back to the variable from which it was originally collected.
From the documentation I understand that onSaveInstanceState() should be called to store only temporary information, and onPause() should be used to store any persistent data.
In the process kill and configuration scenarios they are called. In the normal scenario, there is no need to recreate the activity and neither are called. Note that onSaveInstanceState() is called when your activity goes into the background and NOT when the app process is about to be killed.
Android AlertDialog can be used to display the dialog message with OK and Cancel buttons. It can be used to interrupt and ask the user about his/her choice to continue or discontinue. Android AlertDialog is composed of three regions: title, content area and action buttons.
@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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With