Android - Activity 构造函数与 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.

相关文章