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

发布于 2024-10-09 09:17:07 字数 482 浏览 0 评论 0原文

请帮我解决这个问题。

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.

无论如何,我是否可以处理此异常并将我的活动重定向到主页或重新启动应用程序简历并且不在堆栈中...

Please help me to resolve this problem.

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.

Is there anyway by which i can handle this exception and redirect my activity to Homepage or relaunch application resume and is not in stack...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

身边 2024-10-16 09:17:07

这确实是一个非常好的问题,我试图自己去解答。

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

如果用户手动终止某个应用程序或打开其他 20 个应用程序,他可能希望该应用程序从头开始启动。
为什么 Android 在手机重启后无法恢复状态?

另外,我发现当应用程序(被系统)终止时,全局变量在活动重新启动时变为空。
这就打破了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.

第一点的缺点是需要在每个Activity上进行状态问题处理(Android中还没有实现AOP)。

希望有帮助。

Indeed a very good question which I try to puzzle out myself.

I even wonder What's the difference between killing an app and restarting the phone.

If a user manually kills an app or opens 20 other apps, he'd probably want the app to start from the beginning.
Why Android aren't restoring state after phone restart?

Also, I see that when an app is killed (by the system), global variables turn to null upon activity reinitiating.
This kind of breaks the concept of Java. If you have some kind of state in the application and the application erases it, I would expect the application to restart.

The solution I propose:

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.

The downside of the first point is that the state problem handling needs to be done on every Activity (AOP is not yet implemented in Android).

Hope that helped.

贩梦商人 2024-10-16 09:17:07

使用 SharedPreferences 保存应用程序状态

Use SharedPreferences for save Application state

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文