请帮我解决这个问题。
1 > I am having application which Uses Service to retrieve data from server.
2 > Now on Pressing Home key and i have opened new application..
3 > I have opened around 20 application.
4 > So my application might go out of memory stack.
5 > now i am resuming my application, application crashes as i am opening activity of my
application which is not in the stack.有没有什么办法可以处理这个异常,并将我的活动重定向到主页或重新启动应用程序简历,而不是堆栈中……
发布于 2011-03-08 19:03:41
确实是一个非常好的问题,我试着自己找出答案。
我甚至想知道杀死一个应用程序和重启手机有什么区别。
如果用户手动关闭一个应用程序或打开其他20个应用程序,他可能希望应用程序从头开始。为什么Android在手机重启后不能恢复状态?
此外,我还看到,当应用程序被(系统)终止时,全局变量在活动重新启动时变为null。这在某种程度上打破了Java的概念。如果你在应用程序中有某种状态,而应用程序删除了它,我希望应用程序重新启动。
我提出的解决方案是:
1. Handle the case of a state problem: Initiate a simple state (new Object()) as a global variable. For each Activity, in the methods onCreate/Start/Resume check that the state is null. If it's null launch the first activity with 'Intent.FLAG_ACTIVITY_CLEAR_TOP' - as if the application is relaunched.
2. Try not to use global variables - Always put data in the intent.
3. Lazy load global variables - If you do want to use global data, don't count on one time initialization. Lazy load them 'if (A.MY_DATA == null) {A.MY_DATA = new ...}' - Don't forget to do it in the background if it will take a long time (AsyncTask). Loading partial state needs to be done carefully because it may not comply to other loaded state objects.第一点的缺点是,状态问题处理需要在每个活动上完成(AOP还没有在Android中实现)。
希望这能帮上忙。
发布于 2010-12-30 15:22:35
使用SharedPreferences保存应用程序状态
https://stackoverflow.com/questions/4560714
复制相似问题