Does android save static variables?

允我心安 提交于 2019-12-07 06:39:50

问题


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.

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

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)

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

(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?


回答1:


May This Help you:

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.

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.

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.

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




回答2:


Your activity is being resumed. If you want to control what happens, implement onResume().

See Managing the Activity Lifecycle for details.

EDIT:

Static variables are a Java concept. static just means that there is only one copy of the variable for the whole class. The alternative is that each object would have it's own copy.

So while your code is running, you just have one copy of the variable in your program. It doesn't get saved anyplace, unless you add code to do that.




回答3:


Static variables are associated with a class and they will live as long as the class is in the memory,and destroy when class gets unloaded (which very rarely happens). It can happen when-

 -You force stop your app.
 -Application crashes.
 -You clear your app data.
 -Switch off your Device(Shutdown DVM).


来源:https://stackoverflow.com/questions/18050489/does-android-save-static-variables

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!