android是否保存静态变量? [英] Does android save static variables?

查看:173
本文介绍了android是否保存静态变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个简单的android应用程序,该应用程序基本上是对android文档中可用的片段演示的修改. 在该应用程序中,有一个名为Ipsum.java的文件,该文件具有名为Headlines的字符串的静态ArrayList.

I'm writing a simple android application which is basically a modification of the fragments demo available in the android documentation. In the app, there is a file called Ipsum.java which has a static ArrayList of Strings called Headlines.

在主要活动的onCreate()方法中,我有以下代码将一些元素添加到数组列表中.

In the onCreate() method of the main activity, I have the following code which adds some elements into the array list.

    if (savedInstanceState == null){
        Ipsum.Headlines.add("String 1 ");
        Ipsum.Headlines.add("String 2");
    }

savedInstanceState是一个Bundle,如果正在从某个非活动状态恢复应用,则系统会将其传递给该方法.逻辑是,如果saveInstanceState为null,则不会恢复该应用程序,而是将其作为新实例启动.

savedInstanceState is a Bundle that the system passes to the method, if the app is being resumed from some inactive state. The logic is that if savedInstanceState is null, then the app is not being resumed but being started as a new instance.

如果我使用主页"按钮离开应用程序并重新进入应用程序,则arrayList仅包含两个元素:字符串1"和字符串2". (这是期望的行为)

If I leave the app using the "Home" button and re-enter the app, the arrayList contains only the two elements: "String 1" and "String 2". (This is the desired behavior)

但是,如果我使用后退按钮离开应用程序,然后重新进入应用程序,则会再次 添加"String 1"和"String 2"元素>.然后,该数组具有4个元素.

However, if I leave the app using the back button, and then re-enter the app, the "String 1" and "String 2" elements are added again. The array then has 4 elements.

String 1
String 2
String 1
String 2

(可以看到arrayList的内容,因为它们用于填充listView) 似乎当按下后退按钮时,该应用程序正在存储静态数组列表的内容..当重新启动该应用程序时,未将Bundle传递给onCreate()方法.有人可以根据应用程序的生命周期解释这里发生了什么吗?

(The contents of the arrayList can be seen because they are used to populate a listView) It seems that the app is storing the contents of the static array list when the back button is pressed.. and that a Bundle is not passed to the onCreate() method when the app is restarted. Can someone explain what's happening here, in terms of the app life cycle?

推荐答案

这可能对您有帮助:

让我们从一些背景开始:启动应用程序时会发生什么?

操作系统启动一个进程,并为其分配唯一的进程ID,并分配一个进程表.每个应用程序都在DVM中运行. DVM管理类的加载,实例生命周期,GC等.

Lets start with a bit of background: What happens when you start an application?

The OS starts a process and assigns it a unique process id and allocates a process table.A process start an instance of DVM(Dalvik VM); Each application runs inside a DVM. A DVM manages class loading unloading, instance lifecycle, GC etc.

静态变量的生存期:当JVM加载类时,该静态变量存在,而在卸载该类时,该静态变量死亡.

Lifetime of a static variable: A static variable comes into existence when a class is loaded by the JVM and dies when the class is unloaded.

因此,如果您创建一个android应用程序并初始化一个静态变量,它将保留在JVM中,直到发生以下情况之一:
1.该类已卸载
2. JVM关闭
3.流程终止

So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens:
1. the class is unloaded
2. the JVM shuts down
3. the process dies

请注意,当您切换到另一个应用程序的不同活动时,静态变量的值将保持不变,并且以上三个都不发生.如果以上三种情况中的任何一种发生,则静电将失去其值.

Note that the value of the static variable will persist when you switch to a different activity of another application and none of the above three happens. Should any of the above three happen the static will lose its value.

更多信息:在此链接中阅读 Samuh 的答案...

For More Detail: Read the Answer of Samuh in this Link... Click Here

这篇关于android是否保存静态变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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