活动的实例变量何时初始化? [英] When do activity's instance variables get initialized?
问题描述
在我的android应用程序中,我有一个奇怪的错误.像下面这样的实例变量在运行时意外分配给它的默认值(在这种情况下为false).何时在Android活动中初始化此类变量?
In my android application I have a strange bug. An instance variable like below gets assigned to it's default value unexpectedly at run time (in this case - false). When do this type of variables get initialized in android activity?
public class MainActivity extends Activity {
private boolean isInitialized = false; // <- When do this variable get initialized?
}
推荐答案
创建实例时,将初始化Java中的实例变量.
Instance variables in Java are initialized when the instance is created.
大多数情况下,对象将使用new
实例化.
Most of the time objects would get instantiated with new
.
In case of activities, they are instantiated by the Android framework using reflection (see Instrumentation#newActivity()
). Then initialization for Context
is performed and activity onCreate()
gets called on the instance.
For details, have a look at ActivityThread
source.
这篇关于活动的实例变量何时初始化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!