简体   繁体   中英

onSaveInstanceState()/onRestoreInstanceState() in large application

I've got a complex application: an Application class and a bunch of Activity classes started as Intents from a common Activity.

According to the logs, the process is dying. It happens when I run my app, hit home, start up a whole bunch of other apps and then return to my app.

In the log, I see:

10-25 12:11:08.195: I/ActivityManager(2492): Process my.awesome.app (pid 20860) has died.

my.awesome.app then goes thru the onCreate() method of the main Activity as the app restarts.

However, I end up at the wrong screen because the instance variable that would direct me to the correct screen is not retaining the value it had when the app died. Actually, I'm hoping the app didn't die so much as it was killed by the system and then brought back to life.

Looking at the code, I see no onSaveInstanceState()/onRestoreInstanceState() methods so I'm pretty sure I need to provide them.

The question is which Activities need to provide onSaveInstanceState()/onRestoreInstanceState()/react to what they've done?

Let's say I'm a couple of Activities deep - that is, my Application class has started an Activity class that started a Thread to run an Activity and itself is running another Activity via an Intent.

Do all the involved classes need to provide/react to onSaveInstanceState()/onRestoreInstanceState()? Is it just the Application class since that's where I'm detecting the improper value that sends me to the wrong screen?

onSave/onRestoreInstanceState isn't really for application wide settings. It's more for settings particular to a single activity.

If you always want to go back to a certain activity after a process has been killed, you should probably save the last open state into a preference, and then in onCreate of your main launcher activity, launch the desired activity based on the value of the saved preference.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM