Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: Call super() at the beginning or end of onStart(), onStop(), onDestroy() in activity?

Tags:

Where in onStart(), onStop(), onDestroy() of an activity do I call super.onStart(), super.onStop(), super.onDestroy() ?

like image 862
mrd Avatar asked Aug 12 '12 19:08

mrd


People also ask

When onDestroy () is called before onPause () and onStop () in an Android application?

Answer: onPause() and onStop() will not be invoked if finish() is called from within the onCreate() method. This might occur, for example, if you detect an error during onCreate() and call finish() as a result. In such a case, though, any cleanup you expected to be done in onPause() and onStop() will not be executed.

What is the difference between onStop and onDestroy?

OnDestroy will be called directly from any call to finish() in onCreate, skipping over onStop. onDestroy can be left out after a kill when onStop returns. Starting with Honeycomb, an application is not in the killable state until its onStop() has returned; pre-honeycomb onPause was the killable state.

What is onStart () meant for?

onStart() is called when activity resumes from stopped state. For example, if you have activity A and starts activity B from it, then activity A will be paused ( onPause() ) and then stopped ( onStop() ) and moved to back stack.

When onStop method is called in Android?

onStop() Called when the activity is no longer visible to the user. Either because another Activity has resumed, and is covering this one, an existing activity is coming to the foreground, or the activity is about to be destroyed.


2 Answers

That's my way of calling these super methods:

  • OnCreate(): Definitely the first thing.
  • OnDestroy(): The last thing.
  • OnStop(): The last thing.

However, for the last two, no matter where you call them (in most most cases). So some people prefer to put them at the first to be consistent.

like image 186
iTurki Avatar answered Sep 30 '22 09:09

iTurki


I would call super in the begining, I would probably have the base class complete its work before I do the work of the derived, like the rule in Java

like image 43
Tomer Mor Avatar answered Sep 30 '22 07:09

Tomer Mor