Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What exactly does onDestroy() destroy?

I've been bothered by this "characteristics": When I use Back button to leave my app, I can tell onDestroy() is called, but the next time I run my app, all the static members of the Activity class still retain their values. See the code below:

public class HelloAndroid extends Activity {  private static int mValue;   // a static member here  public void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     TextView tv = new TextView(this);     tv.setText((mValue != 0) ?          ("Left-over value = " + mValue) : "This is a new instance");     setContentView(tv); }  public void onDestroy() {     super.onDestroy();     mValue++; } 

}

The above code displays the left-over value in mValue, and it increments when the session ends so that I know for sure the onDestroy() is called.

I found a useful answer on this forum, and I understand in the above code mValue is a class member, instead of an instance member. But isn't it true that, in this particular case, I have only one single HelloAndroid activity, and so When he dies, everything is cleaned up, and the next time I come back, everything starts over again? (Or, is there some other misterious thing in the system still holds on to it after onDestroy() so that it just won't die???)

(The above is just a variable, what if it's a bunch of objec references? Each piece is a separately re-collectable memory. Is there a chance that GC collects some of them but not all-or-none? This really bugs me.)

like image 302
wwyt Avatar asked Feb 13 '11 01:02

wwyt


People also ask

What is onDestroy () meant for?

onDestroy() is a method called by the framework when your activity is closing down. It is called to allow your activity to do any shut-down operations it may wish to do.

Why onDestroy is called?

onDestroy() should be called at the end because then your code will be called before it is destroyed. Follow this answer to receive notifications.

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.

What is ondestroy and how does it work?

OnDestroy occurs when a Scene or game ends. Stopping the Play mode when running from inside the Editor will end the application. As this end happens an OnDestroy will be executed. Also, if a Scene is closed and a new Scene is loaded the OnDestroy call will be made. When built as a standalone application OnDestroy calls are made when Scenes end.

When is ondestroy () called in Android activity lifecycle?

In the Android Activity Lifecycle's onDestroy docs: onDestroy () is called before the activity is destroyed. The system invokes this callback either because: The Activity#onDestroy () API docs also answers it quite well:

Do I need to override ondestroy?

You don't need to override onDestroy. There is no need to do what sam786 is doing (overriding and just calling the super method) as that is absolutely useless. All other answers seem to go along the lines of "clean up", but don't explain what kind of clean-up or when.

Should I be saving data in ondestroy?

You should not be saving any data in onDestroy (), as you can't guarantee it will be called, so you will lose data sometimes. It won't be called when you press the home button, for example (the case where you want data to be saved). Show activity on this post.


2 Answers

The OS decides when things "go away." The onDestroy is there to let your app have a final chance to clean things up before the activity does get destroyed but it does not mean that the activity will, in fact, be GCed. Here is a good article that I recommend people to read that relates to creating an exit button. While it's not exactly what you asked about, the concepts will help you understand what's going on.

like image 117
Andrew White Avatar answered Sep 24 '22 03:09

Andrew White


You don't just have the Activity though. You also have the application, and its process running in a Dalvik VM. Android will usually leave the application running in the background until it needs to reclaim the memory it is using for some other application. Your static member should remain in memory as long as the process is running. If you try running some memory-intensive application or forcefully closing the running application with some task manager, you may see the static value reset.

like image 36
Jems Avatar answered Sep 25 '22 03:09

Jems