[英]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.我已经为此搜索过,但我能找到的只是人们遇到相反的问题-他们希望他们的应用程序不要重新打开上一个活动,而我试图弄清楚为什么我的应用程序每次都从主要活动开始在系统为 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.
当我在此活动中关闭未保存数据的应用程序,然后 go 并打开其他应用程序以便系统需要终止我的应用程序以释放 memory 时,我丢失了我的数据。 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.
我正在正确使用 onSaveInstanceState/onRestoreInstanceState(当我改变方向或打开省电模式时数据不会丢失),所以我知道我正在正确保存实例数据,问题是 Android 没有打开我的应用程序重新启动时的活动,所以我永远没有机会恢复我保存的实例数据。
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.系统杀死您的应用程序后,您存储在 onSaveInstanceState 中的任何内容也会被删除。
To save state after your app being killed use Shared Preferences or any other Persistent Storage that fits your needs.要在您的应用程序被终止后保存 state,请使用共享首选项或任何其他适合您需要的持久存储。
You should save your state in onStop
And check for a saved state in your onCreate
to restore it if there is one您应该在
onStop
中保存您的 state 并在您的onCreate
中检查已保存的 state 以恢复它(如果有的话)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.