首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android :应用程序由于内存限制而出栈时的状态

Android :应用程序由于内存限制而出栈时的状态
EN

Stack Overflow用户
提问于 2010-12-30 15:06:09
回答 2查看 402关注 0票数 5

请帮我解决这个问题。

代码语言:javascript
复制
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.

有没有什么办法可以处理这个异常,并将我的活动重定向到主页或重新启动应用程序简历,而不是堆栈中……

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2011-03-08 19:03:41

确实是一个非常好的问题,我试着自己找出答案。

我甚至想知道杀死一个应用程序和重启手机有什么区别。

如果用户手动关闭一个应用程序或打开其他20个应用程序,他可能希望应用程序从头开始。为什么Android在手机重启后不能恢复状态?

此外,我还看到,当应用程序被(系统)终止时,全局变量在活动重新启动时变为null。这在某种程度上打破了Java的概念。如果你在应用程序中有某种状态,而应用程序删除了它,我希望应用程序重新启动。

我提出的解决方案是:

代码语言:javascript
复制
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中实现)。

希望这能帮上忙。

票数 5
EN

Stack Overflow用户

发布于 2010-12-30 15:22:35

使用SharedPreferences保存应用程序状态

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/4560714

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档