Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android implementation of lifecycle methods can call the superclass implementation after doing any work? [duplicate]

In the Android documentation we have:

Note: Your implementation of these lifecycle methods must always call the superclass implementation before doing any work...

But I have seen cases where the code is placed after the superclass method, especially for methods like onPause(), onStop(), onDestroy(), for example:

@Override
protected void onPause() {
    LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
    super.onPause();
} 

http://www.vogella.com/articles/AndroidBroadcastReceiver/article.html#ownreceiver_localbroadcastmanager

In both ways it works. So, what's the difference between putting the code before o after calling the superclass method? What's the correct way?

like image 944
adrian4aes Avatar asked Jun 04 '13 19:06

adrian4aes


People also ask

Which of the android lifecycle methods is called when the activity becomes no longer visible to the user?

If the activity becomes completely invisible, the system calls onStop() . The next section discusses the onStop() callback.

What are the life cycle methods of android activity?

An Android activity goes through six major lifecycle stages or callbacks. These are: onCreate() , onStart() , onResume() , onPause() , onStop() , and onDestroy() . The system invokes each of these callbacks as an activity enters a new state.

Which lifecycle method is called only once?

The init() Method The init method is called only once.

Which life cycle methods will call when system kills the process android?

In extreme cases, the system might simply kill your app process without calling the activity's final onDestroy() callback, so it's important you use onStop() to release resources that might leak memory. So, android usually will call onDestroy() of your activity before it is killed but it is not guaranteed.


1 Answers

Directly copied from CommonsWare's this answer. So better give him the upvote

Methods you override that are part of component creation (onCreate(), onStart(), onResume(), etc.), you should chain to the superclass as the first statement, to ensure that Android has its chance to do its work before you attempt to do something that relies upon that work having been done.

Methods you override that are part of component destruction (onPause(), onStop(), onDestroy(), etc.), you should do your work first and chain to the superclass as the last thing. That way, in case Android cleans up something that your work depends upon, you will have done your work first.

Methods that return something other than void (onCreateOptionsMenu(), etc.), sometimes you chain to the superclass in the return statement, assuming that you are not specifically doing something that needs to force a particular return value.

Everything else -- such as onActivityResult() -- is up to you, on the whole. I tend to chain to the superclass as the first thing, but unless you are running into problems, chaining later should be fine.

But if there is no dependancy, then call the superclass methods anywhere you want.

like image 157
stinepike Avatar answered Oct 23 '22 16:10

stinepike