活动的实例变量何时初始化? [英] When do activity's instance variables get initialized?

查看:117
本文介绍了活动的实例变量何时初始化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的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.

对于活动,它们由Android框架使用反射实例化(请参见

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屋!

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