Android Activity Ondestroy() Is Not Always Called and If Called Only Part of the Code Is Executed

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.

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.

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.

Android: OnDestroy isn't called when I close the app from the recent apps button

As specified in the Android documentation, it is not guaranteed that onDestroy() will be called when exiting your application.

"There are situations where the system will simply kill the activity's hosting process without calling this method"

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

Instead, you can create a service which will be notified when the Task your activities are running inside is destroyed.

Create the service class:

public class ClosingService extends Service {

@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}

@Override
public void onTaskRemoved(Intent rootIntent) {
super.onTaskRemoved(rootIntent);

// Handle application closing
fireClosingNotification();

// Destroy the service
stopSelf();
}
}

Declare / register your service in the manifest (within the application tag, but outside any activity tags):

<service android:name=".services.ClosingService"
android:stopWithTask="false"/>

Specifying stopWithTask="false" will cause the onTaskRemoved() method to be triggered in your service when the task is removed from the Process.

Here you can run your closing application logic, before calling stopSelf() to destroy the Service.

If there is a Instruction after onDestroy , will the Instruction be executed?

The answer to your question is, "It Depends".

The reference documentation says that onDestroy is not guaranteed to be called.

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.

So, yes, you message will be called unless Android destroys the process first.

Documentation also say that you must call through to the super classes implementation...

If you override this method you must call through to the superclass implementation.

...but it doesn't say what order the call should be made. Because of that, I would look at Activiy source to see what it is doing. Here is the (version de jour) source code for onDestroy from Google Source.

protected void onDestroy() {
mCalled = true;
// dismiss any dialogs we are managing.
if (mManagedDialogs != null) {
final int numDialogs = mManagedDialogs.size();
for (int i = 0; i < numDialogs; i++) {
final ManagedDialog md = mManagedDialogs.valueAt(i);
if (md.mDialog.isShowing()) {
md.mDialog.dismiss();
}
}
mManagedDialogs = null;
}
// close any cursors we are managing.
synchronized (mManagedCursors) {
int numCursors = mManagedCursors.size();
for (int i = 0; i < numCursors; i++) {
ManagedCursor c = mManagedCursors.get(i);
if (c != null) {
c.mCursor.close();
}
}
mManagedCursors.clear();
}
// Close any open search dialog
if (mSearchManager != null) {
mSearchManager.stopSearch();
}
}

Though this could change at any time, we can see that onDestroy in the super class will clean up resources (as documentation says). So, does your implementation of onDestroy rely upon any of those resources? That would dictate that your code should be called before super.onDestroy(). If not, then the order doesn't matter (except that your teacher has told you what order to use).

Android - onDestroy() called after onCreate()

Using the function onUserInteraction() can cause some issues if you aren't careful. Pressing a button will actually fire the method twice, during KeyDown and KeyUp events. If using this method to start an activity, tapping the screen will work fine. However, pressing a key will call the method twice, attempting to start the activity twice, simultaneously.

When a two activities are started simultaneously, android recognizes this and kills one of the clones. As logs demonstrated, the first instance is wiped out, but not until after the second instance has been fully created and has window focus. Thus, I was able to see onDestroy() called on my started activity, after onCreate().



Related Topics



Leave a reply



Submit