Java Android - 活动构造函数与 onCreate

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3302177/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 21:54:45  来源:igfitidea点击:

Android - Activity Constructor vs onCreate

javaandroidgarbage-collectionandroid-activityoncreate

提问by idolize

I understand that Android Activitieshave specific lifecycles and that onCreateshould be overridden and used for initialization, but what exactly happens in the constructor? Are there any cases when you could/should override the Activityconstructor as well, or should you never touch it?

我知道 AndroidActivities有特定的生命周期,onCreate应该被覆盖并用于初始化,但是在构造函数中到底发生了什么?是否有任何情况下您也可以/应该覆盖Activity构造函数,或者您不应该触摸它?

I'm assuming that the constructor should never be used because references to Activitiesaren't cleaned up entirely (thus hampering the garbage collector) and that onDestroyis there for that purpose. Is this correct?

我假设永远不应该使用构造函数,因为Activities没有完全清除对的引用(从而阻碍了垃圾收集器),而这onDestroy正是为此目的。这样对吗?

采纳答案by Cheryl Simon

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中做。

回答by Pentium10

You need to override the Constructor when your activity will have custom params or you want to track calls from classes that inherited from.

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

回答by biboMandroid

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. 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!

一句话,构造函数让你模拟一个可以被继承重用的“masteractivity”!

回答by Archimedes Trajano

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 onCreatewould 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. Though android-annotations already does that for me so I rarely have an onCreate()method for my Activity. I still use it in Service though.

因为onCreate()我通常用它来做视图映射到局部变量。虽然 android-annotations 已经为我做到了,所以我很少有onCreate()我的 Activity的方法。我仍然在服务中使用它。

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)

  • they would be part of the view which means it needs to be initialized then onCreate needs to be called

  • they are constants which don't need to be put in the constructor anyway, just a static final would do. This includes Paint and Path constants which can be initialized by a static block

  • 他们将有一个“关闭”方法,您必须在适当的时间(onResume 或 onPause)调用该方法

  • 它们将成为视图的一部分,这意味着它需要被初始化然后 onCreate 需要被调用

  • 它们是无论如何都不需要放入构造函数的常量,只需静态最终即可。这包括可以由静态块初始化的 Paint 和 Path 常量