Android - 活动构造函数与 onCreate [英] Android - Activity Constructor vs onCreate
问题描述
我知道 Android Activities
有特定的生命周期,onCreate
应该被覆盖并用于初始化,但是在构造函数中到底发生了什么?在任何情况下,您是否可以/应该也覆盖 Activity
构造函数,或者永远不要碰它?
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? Are there any cases when you could/should override the Activity
constructor as well, or should you never touch it?
我假设永远不应该使用构造函数,因为对 Activities
的引用没有被完全清除(从而阻碍了垃圾收集器)并且 onDestroy
在那里为了这个目的.这是正确的吗?
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. Is this correct?
推荐答案
我想不出有什么好的理由在构造函数中做任何事情.你永远不会直接构造一个活动,所以你不能用它来传递参数.一般就在onCreate里面做.
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.
这篇关于Android - 活动构造函数与 onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!