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

4

60

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?

Lyrist answered 12/12, 2010 at 19:53 Comment(0)
V
71

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.

Visser answered 12/12, 2010 at 20:1 Comment(2)
where did you find this information? i'm curious about services, etc.Bannerol
Android is OpenSouce... just take a look at the source code :PVisser
T
13

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.

Timtima answered 26/5, 2014 at 23:5 Comment(1)
It makes sense. If you want to do anything you certainly don't want to do it on a half-destroyed (cleaned-up) object.Reikoreilly
D
11

Since we are extending from the base android classes, it is always good approach to let the parent class create and initialize itself first during the creation and let the child uninitialize and free the resource first during shutdown/stopping the components. This is the recommended approach to be followed. however, it entirely depends on the use cases and scenarios.

public void onCreate(Bundle bundle){
   super.onCreate(bundle);
   //perform child class initializations.
}

public void onDestroy(){
   //perform uninitialization and free resource
    super.onDestroy();
}
Dinesh answered 25/2, 2016 at 14:50 Comment(0)
C
5

What's your question? You can do it either way, it depends if you want your superclass's onDestroy() called before yours. Usually I don't think it matters in android.

Also, onDestroy() isn't a destructor. It doesn't actually destroy the object. It's just a method that's called based on a certain state. So your instance is still alive and very well* after the superclass's onDestroy() runs and returns.

*Most likely, android is free to kill the activity at any time, but you can assume it's still there.

Cannular answered 12/12, 2010 at 19:58 Comment(2)
in c++ for example you destroy the super class after you are done with cleaning up your private objects. if you destroy the super class first, I don't think it is a good practice, I even think that the app will crash since the instance will be destroyed!Lyrist
As he said, this isn't a destructor, just telling the component to clean up any state it wants to. The object still remains after the onDestroy() call. Generally the order doesn't matter, and won't cause a crash.Barham

© 2022 - 2024 — McMap. All rights reserved.