Is Ondestroy Not Always 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.

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()

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 always called when android destroys activity to save memory but does not kill App?

Is onDestroy always called when android destroys activity to save memory?

Yes

Documentation:

The final call you receive before your 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.

I want to use Application.ActivityLifecycleCallbacks to monitor how
many activities are there in the back-stack. Can I increment/decrement
counter in onCreate/onDestroy to handle this?

Better to counter in the onStart() and onStop() methods, onCreate() doesn't guarantee visibility. For example if somehow something stopped onStart() from happening.

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.

OnDestroy is not being called after the back button is press

Your onDestroy should be defined as

protected void onDestroy() { ... }

and not

void OnDestroy() { ... }

Java is case-sensitive language.



Related Topics



Leave a reply



Submit