Why use onDestroy () if it is not guaranteed to be called?

According to the android activity life cycle, the only call that needs to be made (if the activity ever leaves the Running state, which is usually expected) is onPause() .

So, I have to assume that there are scenarios in which it makes sense to implement onStop() and onDestroy() , although in reality they are not guaranteed.

I understand that onStop() should be implemented when it is possible that the action will return to the Running state through the Stopped state (why would it do it and not return directly - this is another question).

But the need for onDestroy() , when I can put all the clear / save state in onPause() , I don’t understand.

Can you describe the situation with a real application (i.e., not an analogy with driving a car, etc.), in which it would be advisable to implement onDestroy() ?

+44
android android-activity activity-lifecycle
May 24 '11 at 21:49
source share
2 answers

onDestroy is called if you explicitly call finish (); yourself.

Your main activity calls startActivityForResult on card activity.

Map activity using the LocationListener, the user clicks on the map and selects a local restaurant.

Then, some additional functions are configured, which will be sent back to the main action, then finish () is explicitly called; on itself and onDestroy kills the LocationListener and other variables that you called.

Just found this in the docs

onDestroy () = The last call you receive before your activity is destroyed. This can happen either because the activity ends (someone is called finish () on it, or because the system temporarily destroys this instance of the activity to save space. You can distinguish between these two scenarios with the isFinishing () method.

+25
May 24 '11 at 21:51
source share
β€” -

Can you describe the situation with a real application (that is, not an analogy with driving a car, etc.), in which it would be advisable to implement onDestroy ()?

If you want to commit a configuration change. All this in the SDK: http://developer.android.com/reference/android/app/Activity.html

+3
May 24 '11 at 21:54
source share



All Articles