简体   繁体   中英

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 . Up until now and according to the Guide on the Activity Lifecycle regarding 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. 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.

For example, if we had this heirarchy: Child3 <-- Child2 <-- Child1 <-- Activity

Where Child3 extends Child2 and so on. 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 . 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 .

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() ?

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. Because onCreate() will auotmatically get called whether or not you implemented it.

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.

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:

I'm working through the TensorFlow Lite Android example app code and noticed their entry point class ClassifierActivity does not override onCreate.

Their ClassifierActivity doesn't override onCreate() , but it extends from the CameraActivity which does override/implement onCreate() .

  @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; probably they need to keep the code of the image processing in the ClassifierActivity and off the basic staff of the CameraActivity .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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