16

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
adrian4aes
  • 849
  • 2
  • 14
  • 23

4 Answers4

23

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.

Community
  • 1
  • 1
stinepike
  • 54,068
  • 14
  • 92
  • 112
7

When I interested about this problem, I found this rule:

during any kind of initialization, let the super class do their work first; 
during any kind of finalization, you do your work first

This is logical)

Anton
  • 921
  • 3
  • 12
  • 26
2

When overriding any of the methods, you may need to call the superclass implementation. The rule of thumb is that during initialisation, you should always call the superclass first.

public void onCreate() {
   super.onCreate();
   // do work after super class function
   // setContentView(R.layout.main);
    }

During de-initialisation, you should do the work first before calling the super class.

public void onPause() {
   // do work here first before super class function //LocalBroadcastManager.getInstance(this).unregisterReceiver(mMessageReceiver);
       super.onPause();
    }
Angad Cheema
  • 187
  • 1
  • 5
1

According to Java standards and best practices, the super call should go first. I believe the reason for this is that there may be work that needs to be done in a super that would cause problems in your code if you didn't do these first.

However, I have done work before calling super and haven't had any problems.

I don't have any framework examples but I have a BaseActivity class which extends Activity and all of my Activities extend BaseActivity. I have several methods that need to be implemented in these subclasses. If I don't make the call to super first then some variables don't get initialized that need to be so I would get NPEs

codeMagic
  • 44,549
  • 13
  • 77
  • 93