Android的 - 活动构造VS的onCreate [英] Android - Activity Constructor vs onCreate

查看:148
本文介绍了Android的 - 活动构造VS的onCreate的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,Android的活动有特定的生命周期,而的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? Are there any cases when you could/should override the Activity constructor as well, or should you never touch it?

我假设,构造函数不应该使用,因为引用活动不清理完全(从而妨碍垃圾回收器),以及的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的 - 活动构造VS的onCreate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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