Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onSaveInstanceState/onRestoreInstanceState for a dialog

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?

like image 732
theblitz Avatar asked Nov 04 '22 14:11

theblitz


People also ask

What is onSaveInstanceState () and onRestoreInstanceState () in activity?

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.

What is the difference between the onPause () event and the onSaveInstanceState () event?

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.

Where is onSaveInstanceState called?

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.

What is AlertDialog message?

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.


1 Answers

@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.

like image 180
Arun PB Avatar answered Nov 17 '22 20:11

Arun PB