Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Restore State after process is killed

I have implemented onRestoreInstanceState and onSaveInstanceState. They both work successfully when the app is minimized (by pressing the home button, or another activity is in the foreground). However, when I minimize the app and the kill the process (by opening the list of current processes and ending that one) the app does not restore the saved instance state. How can I make it restore that saved state? Is my best option to write the savedInstanceState bundle to a file using serializable (thus having something stored that on the hard drive, rather then RAM), then reload the savedInstanceState from the file?

like image 424
Rilcon42 Avatar asked Jan 19 '13 00:01

Rilcon42


2 Answers

How can I make it restore that saved state?

You can't. That's not what the saved instance state is for.

Is my best option to write the savedInstanceState bundle to a file using serializable (thus having something stored that on the hard drive, rather then RAM), then reload the savedInstanceState from the file?

Your "best option" is persistence, such as a file, database, or SharedPreferences. Your specific approach seems odd.

Saved instance state is only for temporary information, such as the contents of a form that the user has filled in but not yet submitted. Anything beyond that does not belong in saved instance state, but rather in some sort of persistent data model. This is no different than building a Web app, where the data that you have in your DOM and JavaScript variables are not going to live forever, but instead need to be store somewhere (typically on a Web server).

like image 122
CommonsWare Avatar answered Oct 16 '22 05:10

CommonsWare


It's probably too late for the answer, but you can save state (and it will survive the process killing) with SavedStateHandle from Android Jetpack.

See more here: https://developer.android.com/topic/libraries/architecture/viewmodel-savedstate

like image 20
deviant Avatar answered Oct 16 '22 07:10

deviant