简体   繁体   中英

Android is not reopening previous activity after system has killed app

I've searched SO for this but all I can find is people having the opposite problem - they want their app not to reopen to the previous activity, whereas I'm trying to figure out why my app is starting from the main activity every time I reopen it after the system has killed it for memory.

My problem: I have an activity where the user creates data that eventually gets persisted. When I close the app with unsaved data in this activity, and then go and open other apps so that the system needs to kill my app to free memory, I'm losing my data. I am using onSaveInstanceState/onRestoreInstanceState correctly (the data is not lost when I change orientation or turn on power-saving mode), so I know I'm saving the instance data correctly, the problem is that Android is not opening my app to this activity when I restart, so I never have a chance to restore the instance data I saved.

Any idea why my app would not be opening to the last activity I left it on before the system killed it? Thanks.

After the system kills your app, whatever you stored in onSaveInstanceState is deleted, too.

To save state after your app being killed use Shared Preferences or any other Persistent Storage that fits your needs.

You should save your state in onStop And check for a saved state in your onCreate to restore it if there is one

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