mgibson
mgibson

Reputation: 6213

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 neccessary for compilation but what about all of the constructors?

onResume(), onStop(), onStop() etc. Which overridden methods have important super constructors you need to include?

Is there a list somewhere?

I see the super constructors included in some code, not in others.. I have included them sometimes myself, others not and have never really noticed a difference..

Any light that could be shed would be well received!

Cheers

Upvotes: 0

Views: 569

Answers (3)

Mister Smith
Mister Smith

Reputation: 28168

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.

Upvotes: 2

Raja Asthana
Raja Asthana

Reputation: 2100

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

Upvotes: 2

StarPinkER
StarPinkER

Reputation: 14271

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.

public class Activity extends ApplicationContext {
    protected void onCreate(Bundle savedInstanceState);

    protected void onStart();

    protected void onRestart();

    protected void onResume();

    protected void onPause();

    protected void onStop();

    protected void onDestroy();
}

Upvotes: 2

Related Questions