Android - Activity 构造函数与 onCreate [英] Android - Activity Constructor vs onCreate

查看:71
本文介绍了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.

这篇关于Android - Activity 构造函数与 onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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