Skip to content Skip to sidebar Skip to footer

Android - Which Activity Methods Require A Super Constructor?

In most Android apps, onCreate() is overridden with the first bit of code being super.onCreate(savedInstanceState) and I know this gathers the savedInstanceState Bundle and is necc

Solution 1:

onCreate(), onStart() and onResume() are used to startup the activity while on onStop() and onDestroy() are used to stop or clean up the activity.

As per the documentation you need to call the super for each of the method.

Derived classes must call through to the super class's implementation of this method. If they do not, an exception will be thrown.

For more info

Solution 2:

Check this documentation.

The entire lifecycle of an activity is defined by the following Activity methods. All of these are hooks that you can override to do appropriate work when the activity changes state. All activities will implement onCreate(Bundle) to do their initial setup; many will also implement onPause() to commit changes to data and otherwise prepare to stop interacting with the user. You should always call up to your superclass when implementing these methods.

publicclassActivityextendsApplicationContext {
    protectedvoidonCreate(Bundle savedInstanceState);

    protectedvoidonStart();

    protectedvoidonRestart();

    protectedvoidonResume();

    protectedvoidonPause();

    protectedvoidonStop();

    protectedvoidonDestroy();
}

Solution 3:

If you read the source code, you'll notice how in Activity.java those 6 methods have code inside, so I'd say you should call super for every overriden onXXX method.

Post a Comment for "Android - Which Activity Methods Require A Super Constructor?"