Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 877
Vassilis Avatar asked Dec 12 '10 19:12

Vassilis


People also ask

What does Super onDestroy do?

This is what super. onDestroy does (in that order): Dismiss any dialogs the activity was managing. Close any cursors the activity was managing.

What happens when onDestroy is called?

If onDestroy() is called as the result of a configuration change, the system immediately creates a new activity instance and then calls onCreate() on that new instance in the new configuration. The onDestroy() callback should release all resources that have not yet been released by earlier callbacks such as onStop() .

When onDestroy method is called in Android?

onDestroy( ) is called before the activity is destroyed. The system invokes this callback either because: the activity is finishing (due to the user completely dismissing the activity or due to finish( ) being called on the activity), or.

When your app goes into the background it's not guaranteed that onDestroy is called True or false?

As specified in the Android documentation, it is not guaranteed that onDestroy() will be called when exiting your application. Instead, you can create a service which will be notified when the Task your activities are running inside is destroyed.


2 Answers

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.

like image 78
Cristian Avatar answered Sep 22 '22 14:09

Cristian


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.

like image 37
Zorfling Avatar answered Sep 25 '22 14:09

Zorfling