小问题,但我无法在文档中找到答案。
Android 应用中静态字段的生命周期是多长? 什么时候初始化,什么时候销毁? final 属性是否修改了它的生命周期?私有(private)/公共(public)呢?只要 Application 实例存在,它们就存在吗?
举两个例子:
public class DemoClass {
static int one = 1;
static int three = DemoActivity.two + one;
}
public class DemoActivity extends Activity {
public static int two = DemoClass.one + DemoClass.one;
private static final int four;
public static int five;
public void onCreate(Bundle b) {
four = two + two;
five = DemoClass.three + DemoClass.one + DemoClass.one;
}
}
编辑:还有静态字典呢?
例如,
public class AnotherDemoActivity extends Activity {
public static ArrayList<String> strings = new ArrayList<String>();
@Override public void onCreate(Bundle b) {
strings.add("test");
strings.add(new String("another test");
strings.add(new DemoClass());
}
“字符串”元素会存在多长时间?
最佳答案
When is it initialized and when is it destroyed?
它在加载声明它的类时被初始化,它将一直存在到您的应用程序进程结束。
Does the final attribute modify its lifecycle?
没有
What about private/public?
没有
Do they live as long as the Application instance is alive?
是
关于java - Android 中的静态场生命周期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12692106/