11

When creating own Activity subclass, we are overriding some of the basic Activity lifecycle functions.

In which of these we must call super implementation, where we should and where is it only good manner ?

// base lifecycle
onCreate(Bundle savedInstanceState);
onStart();
onRestart();
onResume();
onPause();
onStop();
onDestroy();
finalize();
onUserLeaveHint();
// instance state
onSaveInstanceState(Bundle outState);
onRestoreInstanceState(Bundle savedInstanceState)
// others
onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
onCreateOptionsMenu(Menu menu);
onOptionsItemSelected(MenuItem item);
onContextItemSelected(MenuItem item);
onMenuItemSelected(int featureId, MenuItem item);
onActivityResult(int requestCode, int resultCode, Intent data);
onBackPressed();

List of methods is not final, feel free to add more functions, where we should know, what to do.


For those who are interested in, where should be super.methodName placed
Locations of super() calls in Android Eclipse Plugin generated code reliable?

Community
  • 1
  • 1
Marek Sebera
  • 39,650
  • 37
  • 158
  • 244
  • Android will throw a RuntimeException if you forget to call the super method although it would be necessary. – Flo Jan 17 '12 at 09:47
  • Superclass at FIRST: `onRestoreInstanceState، onStop, onRestart, onStart`. Superclass at LAST: `onDestroy, onPause, onSaveInstanceState, onResume`. – Yousha Aleayoub Sep 04 '16 at 15:30

4 Answers4

6

must:

  • onCreate(Bundle savedInstanceState);
  • onStart();
  • onRestart();
  • onResume();
  • onPause();
  • onStop();
  • onDestroy();
  • onPostCreate(Bundle savedInstanceState);
  • onPostResume();

should / shouldn't: (may be helpful to call superclass method, unless you manage activity's state or deliberately alter activity's behavior yourself - in such case it may be harmful)

  • onSaveInstanceState(Bundle savedInstanceState);
  • onRestoreInstanceState(Bundle savedInstanceState);
  • onBackPressed();
  • onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo);
  • onCreateOptionsMenu(Menu menu);
  • onOptionsItemSelected(MenuItem item);
  • onContextItemSelected(MenuItem item);
  • onMenuItemSelected(int featureId, MenuItem item);

Actually, when overriding any method, it's a good practice to look at the source code of the overridden class and see what exactly this method does.

a.ch.
  • 8,285
  • 5
  • 40
  • 53
  • 1
    great, at least you, answered my question :) Thanks – Marek Sebera Jan 17 '12 at 10:36
  • would you please explain how you categorized these methods into "must" and "should"? I am a beginner and I am trying to understand how to figure out which methods should have a call to super(). I don't want to just remember and recall when needed... I want to understand how to figure it out – Jeet Parekh Dec 14 '15 at 07:18
  • I've slightly updated the answer. Basically, there are methods you must call (you'll know it from the methods' description, from Lint hints or from exceptions you'll get) and methods that affect the behavior of the Activity/Fragment in a certain way. – a.ch. Dec 14 '15 at 11:00
  • Superclass at FIRST: `onRestoreInstanceState، onStop, onRestart, onStart`. Superclass at LAST: `onDestroy, onPause, onSaveInstanceState, onResume`. – Yousha Aleayoub Sep 04 '16 at 15:30
4

Its just a good practice to call the super implementation.

BUT some times its mandatory, like in "onCreate()", else there will be an "ActivityInstantiateingexcepetion"in this case.

Also, some times you want to override the super implementation in some cases, like

onBackPressed(){
if(myFlag){
// do my stuff
}
else {
// Do usual stuff on Back pressed
super.onBackPressed ();
}

So this way if your "myFlag" will be true, your stuff will be done else normal onBackpressed will be executed.

akkilis
  • 1,904
  • 14
  • 21
2

Its all Activity class method.In Java and Android if you call superclass method ,You must write super.methodName

Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134
0

all these methods should call super.method() I think

JohnCookie
  • 661
  • 3
  • 7