简体   繁体   English

什么时候可以在入口点活动类中省略对 onCreate 的调用?

[英]When is it acceptable to omit a call to onCreate within an entrypoint activity class?

I'm working through the TensorFlow Lite Android example app code and noticed their entry point class ClassifierActivity does not override onCreate .我正在研究TensorFlow Lite Android 示例应用程序代码,并注意到它们的入口点类ClassifierActivity没有覆盖onCreate Up until now and according to the Guide on the Activity Lifecycle regarding onCreate()到目前为止,根据关于 onCreate() 的活动生命周期指南

You must implement this callback, which fires when the system first creates the activity.您必须实现此回调,它会在系统首次创建活动时触发。

I thought onCreate must be called/overridden in the entry point activity.我认为必须在入口点活动中调用/覆盖onCreate By entry point activity I mean the one specified in the manifest as such.我所说的入口点活动是指清单中指定的活动。

From a simple test of an empty activity, it seems Android simply calls the onCreate method from the lowest available child available child class.从对空活动的简单测试来看,Android 似乎只是从可用的最低可用子类调用onCreate方法。

For example, if we had this heirarchy: Child3 <-- Child2 <-- Child1 <-- Activity例如,如果我们有这样的层次结构: Child3 <-- Child2 <-- Child1 <-- Activity

Where Child3 extends Child2 and so on. Child3 扩展 Child2 的地方,依此类推。 If only Child1 and Activity contained calls to onCreate and we pointed our manifest to Child3 only, it would first call the onCreate method of Child1 .如果只有Child1和活动包含到电话onCreate ,我们提出我们的清单只Child3,它会首先调用onCreate的方法Child1 This makes sense from an OOP perspective, but for some reason I feel like Android apps would not build without overriding this before and it threw me off to see an entry point activity without an overriding call to onCreate .从 OOP 的角度来看,这是有道理的,但出于某种原因,我觉得 Android 应用程序不会在没有覆盖它之前构建,它让我无法看到没有覆盖调用onCreate的入口点活动。

Has this always been the case and I just didn't realize it or was the ability to omit the overriding call something added in at some point?情况一直如此,我只是没有意识到这一点,或者是否能够在某些时候添加一些内容来忽略覆盖调用? Is this bad practice?这是不好的做法吗? Does this prevent you from having a connection between your child and parent classes via a Bundle or Context , which you'd normally pass via super.onCreate() ?这是否会阻止您通过BundleContext在您的子类和父类之间建立连接,您通常通过super.onCreate()传递?

You must implement this callback, which fires when the system first creates the activity.您必须实现此回调,它会在系统首次创建活动时触发。

This doesn't mean that an app won't launch if it didn't implement onCreate() method, but it means that it must be implemented if you want to show something different on the screen than the default.这并不意味着如果应用程序没有实现onCreate()方法就不会启动,而是意味着如果您想在屏幕上显示与默认值不同的内容,则必须实现它。 Because onCreate() will auotmatically get called whether or not you implemented it.因为无论您是否实现了onCreate()都会自动被调用。

That apparently described in the next phrase:这显然在下一个短语中描述:

In the onCreate() method, you perform basic application startup logic that should happen only once for the entire life of the activity.在 onCreate() 方法中,您执行基本的应用程序启动逻辑,该逻辑应该在整个活动生命周期内只发生一次。

Running an app without overriding any lifecycle callback methods will run normally;在不覆盖任何生命周期回调方法的情况下运行应用程序将正常运行; running the following code will have no issues at all:运行以下代码将完全没有问题:

class MainActivity : AppCompatActivity() {
}

For the TensorFlow example:对于 TensorFlow 示例:

I'm working through the TensorFlow Lite Android example app code and noticed their entry point class ClassifierActivity does not override onCreate.我正在研究 TensorFlow Lite Android 示例应用程序代码,并注意到它们的入口点类 ClassifierActivity 没有覆盖 onCreate。

Their ClassifierActivity doesn't override onCreate() , but it extends from the CameraActivity which does override/implement onCreate() .他们的ClassifierActivity不会覆盖onCreate() ,但它从覆盖/实现onCreate()CameraActivity扩展而来。

  @Override
  protected void onCreate(final Bundle savedInstanceState) {
    LOGGER.d("onCreate " + this);
    super.onCreate(null);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    setContentView(R.layout.tfe_ic_activity_camera);

    if (hasPermission()) {
      setFragment();
    } else {
      requestPermission();
    }
//....

Here CameraActivity is just considered a base activity where the basic UI stuff exist;此处CameraActivity仅被视为存在基本 UI 内容的基础活动; probably they need to keep the code of the image processing in the ClassifierActivity and off the basic staff of the CameraActivity .可能他们需要将图像处理的代码保留在ClassifierActivity ,而不是CameraActivity的基本人员。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM