7
Android Studio 0.4.6

Hello,

I have a code snippet here. And I have often been confused if super should be the first line of execution or the last. Normally, I make it the first call so that the default properties can be set in the parent class. However, I was looking at some code that does it before. I am just wondering does it make any difference?

  @Override
    protected void onDestroy() {
        mBroadCastMgr.unregisterReceiver(receiver); 
        super.onDestroy();
    }
ant2009
  • 27,094
  • 154
  • 411
  • 609

6 Answers6

8

As in the docs:

onDestroy() = This callback is called before the activity is destroyed by the system.

Sample Illustration:

/** Called just before the activity is destroyed. */
   @Override
   public void onDestroy() {
      super.onDestroy();
      Log.d(msg, "The onDestroy() event");
   }

Popularly mentioned by user @Cristian (don't know where)-

It really depends on what you want to do in your onDestroy. This is what super.onDestroy does (in that order):

1) Dismiss any dialogs the activity was managing.

2) Close any cursors the activity was managing.

3) Close any open search dialog.

Having said that,

"If the logic you put inside onDestroy has something to do with those three things that android does, then you may have to worry about the order. Otherwise, and in most of the cases, it does not matter."

sjain
  • 23,126
  • 28
  • 107
  • 185
4

In general when you are creating something your code goes last when you are destroying something your code goes first. It is like in real life imagine adding something to something existing and removing something from something existing

In onDestroy, definitely, place your code first the activity is not valid after super.onDestory

Another interesting situation in onSaveInstanceState you should place your code first because it is super.onSaveInstanceState who does the actual saving (and you thus add what extra you want to save, first)

Alexander Kulyakhtin
  • 47,782
  • 38
  • 107
  • 158
  • 2
    that's not exactly true. maybe you want override some kind of behaviour of the parent class in the child – Blackbelt Feb 28 '14 at 09:34
  • Your argument is valid only if we do know what super.onDestroy does while commonly we do not or do not want to. Usually we do something unrelated to the standard destroying process and so we wnat the base process to go last so it goes as if nothing else ever happened – Alexander Kulyakhtin Feb 28 '14 at 09:42
2

If you want to implement cleanup logic safely, you can use next pattern:

@Override
protected void onDestroy() {
    try {
        mBroadCastMgr.unregisterReceiver(receiver);
    } finally {
        super.onDestroy();
    }
}

In general, necessarity of super class call should be checked in all overriden methods using documentation (method javadoc) or super class' source code.

zealot
  • 237
  • 1
  • 4
1

You should call it before, because else perhaps the super class is overwriting some of your modifications. Means if you call it before, your modifications are the last one and will be taken.

ReeCube
  • 2,545
  • 17
  • 23
1

If the derived class method is creating/updating the object information, then it's better to be the first line.

If you are free/releasing the object, then at the end use:

@Override
protected void onInit() {
    super.onInit();
}
RobF
  • 2,758
  • 1
  • 20
  • 25
0

As @ReeCube already suggested, generally you want to call super implementation before executing your code to ensure overwriting super implementation and not the other way around. But there is at least one exception that I can think of when you should call the super implementation after yours. Imagine you are constructing objects. Naturally, you want to start with super implementation and then yours. There is of course symmetric operation to constructing and that is destroying objects. It is only logical that you do want to destroy your mess first and after that call your parent class to deal with the rest it created itself (recursive approach), so when you are destroying objects you should execute your code first and call super class implementation after.

arenaq
  • 2,304
  • 2
  • 24
  • 31