Android不销毁后退按钮数据 [英] Android does not destroy data on back button

查看:90
本文介绍了Android不销毁后退按钮数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在的onCreate()方法予加载级别的列表。这被存储在一个单结构(使用一个枚举为这个)。当我preSS后退按钮并再次返回,名单仍然存在,所有的水平再次增加。我不明白为什么会这样,因为活动生命周期指出,之前的onCreate()再次被调用的过程中被杀死。

这是怎么回事?

修改,一些code:

在单独的文件:

 公开枚举GameInformation {
   实例;
       公开名单<平>的Levelset;

       公共无效loadLevelSet(上下文的背景下){
       ...
       }
}
 

在主要活动:

 公共无效的onCreate(包savedInstanceState){
    GameInformation.INSTANCE.loadLevelSet(本);
}
 

解决方案
  

我不明白为什么会这样,因为活动生命周期状态的过程的onCreate(前被杀)再次被调用。

没有,没有。

这个过程肯定不会立即杀死,当你的最后一项活动是通过BACK按钮销毁。 Android将保持过程身边,即使你有没有活动部件,如果用户恰好想快速返回到应用程序。后来,当机器人需要的RAM,它就会终止该进程。

您不应该做任何假设,任何是否静态数据成员会或不会在那里。只是懒惰初始化他们,如果他们是空的。

In the onCreate() method I load a list of levels. This is stored in a singleton structure (using an enum for this). When I press the back button and return again, the list is still there and all the levels are added again. I don't understand why this is happening, since the Activity Lifecycle states that the process is killed before onCreate() is called again.

Why is this happening?

edit, some code:

In separate file:

public enum GameInformation {
   INSTANCE;
       public List<Level> levelSet;

       public void loadLevelSet(Context context) {
       ...
       }
}

In main activity:

public void onCreate(Bundle savedInstanceState) {
    GameInformation.INSTANCE.loadLevelSet(this);
}

解决方案

I don't understand why this is happening, since the Activity Lifecycle states that the process is killed before onCreate() is called again.

No, it does not.

The process is most certainly not killed immediately when your last activity is destroyed via the BACK button. Android will keep the process around, even if you have no active components, in case the user happens to want to return quickly to the app. Later on, when Android needs RAM, it will then terminate the process.

You should not be making any assumptions whatsoever as to whether static data members will or will not be there. Just lazy-initialize them if they are null.

这篇关于Android不销毁后退按钮数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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