Why does super.onDestroy() in java Android goes on top in destructors? [duplicate]

According to which logic does super.onDestroy(); in destructors goes on top? For example:

protected void onDestroy() {        
    super.onDestroy();
    releaseMediaPlayer();
}

and not:

protected void onDestroy() {        
    releaseMediaPlayer();
    super.onDestroy();
}

Like in c++, obj-c, pascal, etc?


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

  • Dismiss any dialogs the activity was managing.
  • Close any cursors the activity was managing.
  • Close any open search dialog

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.


In the ThreadSample.zip on the Reporting Work Status training, there is a comment in onDestroy()

public void onDestroy() {
    ...
    // Must always call the super method at the end.
    super.onDestroy();
}

So perhaps when using Broadcast Receivers, the super must go at the end.