Activity Ondestroy Never Called

Android Activity onDestroy() is not always called and if called only part of the code is executed

Take a look at this:

Activity OnDestroy never called?

And this:

http://developer.android.com/reference/android/app/Activity.html#onDestroy%28%29

Basically, there's never a guarantee that onDestroy() will be called, and in some cases processes such as your app will be killed directly, bypassing the method call anyway.

Activity OnDestroy never called?

onDestroy() is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.

So, to test your code() you can make a test button, that will call finish() on your activity.

Read more here.

Also, I believe you don't need to call all this stuff in onDestroy() until adap is not a critical resource. And even in that case android system has mechanisms to properly dispose them.

onDestroy never called

onDestroy() is called only when system is low on resources(memory, cpu time and so on) and makes a decision to kill your activity/application or when somebody calls finish() on your activity.

So, to test your code() you can make a test button, that will call finish() on your activity.

Read more here.

Also, I believe you don't need to call all this stuff in onDestroy() until adap is not a critical resource. And even in that case android system has mechanisms to properly dispose them.

Its already answered here

Is ondestroy not always called?

From Android developer documentation:

protected void onDestroy ()

Added in API level 1 Perform any final cleanup before an activity is
destroyed. This can happen either because the activity is finishing
(someone called finish() on it, or because the system is temporarily
destroying this instance of the activity to save space. You can
distinguish between these two scenarios with the isFinishing() method.

Note: do not count on this method being called as a place for saving
data! For example, if an activity is editing data in a content
provider, those edits should be committed in either onPause() or
onSaveInstanceState(Bundle), not here. This method is usually
implemented to free resources like threads that are associated with an
activity, so that a destroyed activity does not leave such things
around while the rest of its application is still running. There are
situations where the system will simply kill the activity's hosting
process without calling this method (or any others) in it, so it
should not be used to do things that are intended to remain around
after the process goes away.

You can move your code to onPause() or onStop()

Android onDestroy() method not working as expected

First of all the documentation says that the onDestroy() method:

should not be used to do things that are intended to remain around after the process goes away.

and:

This method is usually implemented to free resources like threads that are associated with an activity

Also at the beginning of the docs on the Activity there is a note that the methods onDestroy() and onStop() are killable:

methods that are marked as being killable, after that method returns the process hosting the activity may be killed by the system at any time without another line of its code being executed.

And there is a recommendation to use the onPause() method instead of those that are killable.

So apparently there is some android internal timeout for the onDestroy() function that might be controlling if nothing too long is happening in it and if it does, then it probably kills the execution. This is my guess.

Based on what was in the documentation I would refrain from using this method to do what You are doing here and I would try using the onPause() method instead.

If the code is meant to be executed only when the applicatoin is finishing then use the isFinishing() method to check for it. The docs recommend this approach:

Check to see whether this activity is in the process of finishing, either because you called finish() on it or someone else has requested that it finished. This is often used in onPause() to determine whether the activity is simply pausing or completely finishing.

in which scenarios onDestroy() is not called?

Suppose there are two activities A and B.Activity A contains a next button and B contains a back button.If you click next,then Activity B will be launched.At that point of time onDestroy() is not called.onStop() of activity A is called after onResume() of B.

Then when you click the back button in Activity B,then onRestart() of A will called after onStop() of B.In this case also onDestroy() will be not called.

When you press the back button of the device onDestroy() will be called at that point of time or if you call finish() while navigating from A to B.



Related Topics



Leave a reply



Submit