安卓应用程序的状态,当应用程序退出堆栈由于内存约束 [英] Android : Application's state when application is out of stack due to Memory constrain

查看:218
本文介绍了安卓应用程序的状态,当应用程序退出堆栈由于内存约束的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮我解决这个问题。

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...

推荐答案

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

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

我甚至不知道什么是杀一个应用程序,并重新启动手机之间的差异。
如果用户手动杀死一个应用程序或打开其他20个应用程序,他很可能希望应用程序从头开始。 为什么Android手机重启后没有恢复状态?

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?

另外,我看到,当一个应用程序被杀害(由系统),全局变量在活动重新发起转空。 这种断裂的Java的概念。如果你有某种状态的应用程序和应用程序删除它,我希望在应用程序重新启动。

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.

该解决方案,我建议:

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的)。

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).

希望有所帮助。

这篇关于安卓应用程序的状态,当应用程序退出堆栈由于内存约束的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆