简体   繁体   English

Android - 活动构造函数vs onCreate

[英]Android - Activity Constructor vs onCreate

I understand that Android Activities have specific lifecycles and that onCreate should be overridden and used for initialization, but what exactly happens in the constructor? 我知道Android Activities具有特定的生命周期,并且应该重写onCreate并将其用于初始化,但构造函数中究竟发生了什么? Are there any cases when you could/should override the Activity constructor as well, or should you never touch it? 是否有任何情况可以/应该覆盖Activity构造函数,或者你是否应该触摸它?

I'm assuming that the constructor should never be used because references to Activities aren't cleaned up entirely (thus hampering the garbage collector) and that onDestroy is there for that purpose. 我假设永远不应该使用构造函数,因为对Activities引用不会被完全清除(因此妨碍了垃圾收集器)并且onDestroy就是为此目的而存在的。 Is this correct? 它是否正确?

I can't think of any good reason to do anything in the constructor. 我想不出有任何理由在构造函数中做任何事情。 You never construct an activity directly, so you can't use it to pass in parameters. 您永远不会直接构造活动,因此您无法使用它来传递参数。 Generally, just do things in onCreate. 一般来说,只需在onCreate中做一些事情。

A good reason for putting things in the constructor as Gili's comment had stated is the use of final fields. 像Gili的评论所说的那样,把事情放在构造函数中的一个很好的理由是使用final字段。

However, if you initialize things in the constructor, then the lifespan of the object will be a little bit longer, though I don't think by much because the onCreate would be called shortly thereafter. 但是,如果你在构造函数中初始化东西,那么对象的生命周期会稍微长一些,尽管我没有多想,因为之后很快就会调用onCreate

Although it's against my ideal, I do avoid the constructor for initialization of the activity members and rely on onResume() and onPause() for resources that my app is dealing with. 虽然它违背了我的理想,但我确实避免了构造函数初始化活动成员,并依赖onResume()onPause()来处理我的应用程序正在处理的资源。

For onCreate() I usually use it to do view mapping to local variables. 对于onCreate()我通常使用它来查看映射到局部变量。 Though android-annotations already does that for me so I rarely have an onCreate() method for my Activity. 虽然android-annotations已经为我做了,所以我很少有一个onCreate()方法用于我的Activity。 I still use it in Service though. 我仍然在服务中使用它。

However, if you look at the members you may be initializing 但是,如果您查看可能正在初始化的成员

  • they would have a "close" method that you have to invoke at the proper time (onResume or onPause) 他们会有一个“关闭”方法,你必须在适当的时间调用(onResume或onPause)

  • they would be part of the view which means it needs to be initialized then onCreate needs to be called 它们将成为视图的一部分,这意味着它需要被初始化然后onCreate需要被调用

  • they are constants which don't need to be put in the constructor anyway, just a static final would do. 它们是常量,无论如何都不需要放在构造函数中,只需要一个静态的final。 This includes Paint and Path constants which can be initialized by a static block 这包括可以通过静态块初始化的Paint和Path常量

I am now on a case that needs to override the constructor. 我现在处于需要覆盖构造函数的情况。 In fact, I have some activities that have the same structure. 事实上,我有一些具有相同结构的活动。 So instead of creating many activities, I'll create one "Master" activity and the others will inherit this one. 因此,我将创建一个“Master”活动而不是创建许多活动,而其他活动将继承此活动。 So I need to override the constructor of the child activity to be able to initialize some variables that will be used in the oncreate methods. 所以我需要覆盖子活动的构造函数,以便能够初始化将在oncreate方法中使用的一些变量。

In two words, the constructor makes you simulate a "masteractivity" that can be reused by inheritance! 用两个词来说,构造函数可以让你模拟一个可以通过继承重用的“materactivity”!

当您的活动具有自定义参数或您想要跟踪从其继承的类的调用时,您需要覆盖构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM