Activity OnDestroy never called?
Asked Answered
P

3

38

I am using following code in my ListActivity

// a separate class in project
public class MyActivity extends ListActivity {
    // some common functions here..
}

public class SelectLocation extends MyListActivity {

    public void onCreate(Bundle savedInstance) {
        // here.....
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (adap != null) adap = null;
        if (list != null) list = null;
        System.gc();
    }
}

any one guide me why onDestroy method is not called in my code?

Popover answered 15/12, 2010 at 12:40 Comment(1)
The if checks are superfluous - adap = list = null;Relational
P
64

onDestroy() is called only when the system is low on resources (memory, cpu time, etc) 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 the android system has mechanisms to properly dispose of them.

Pliocene answered 15/12, 2010 at 12:43 Comment(4)
onDestroy is not only called when the system is low on resources. It is also called when an Activity is not reachable anymoreColumelliform
Its also called whenever you rotate the device since its characteristics have changed (i.e. orientation) which could affect what resources are used, so it destroys and creates the Activity. After onDestroy() then onCreate() gets called.Criminate
So how to know when the user is killing the app?Fasta
Why is this 60 upvotes and correct answer when it's completely wrong about when onDestroy is called?Greenhouse
C
31

There is no guarantee that your onDestroy method will be called at all.

The code that you are using in your onDestroy method is not needed at all. If destroy is called your acitivity will be removed from the stack and is free for garbage collection anyway with all the resources in it that are only referenced by the activity. Also System.gc() is supposed to be bad style. On Android the system nearly always knows when it is the best time to do a garbage collection. Most of the times an activity finishes garbage collection is triggered automatically. Just remove the whole onDestroy method. If you have problems with the overall memory of your application the problem is somewhere else.

Caloric answered 15/12, 2010 at 12:52 Comment(6)
Its sort of funny, but the image here sort of says that onDestroy will always be called. If you read the text, that is also said: "The entire lifetime of an activity happens between the call to onCreate() and the call to onDestroy()." developer.android.com/guide/topics/fundamentals/…Sheave
Yes in the beginning of the documentation and the image it is not shown very clearly that onDestroy might not be called. It is only mentioned later while the killable after part of the table below the image is explained. If you only have a quick glance at the image you may be misled.Caloric
Actually, you can read quite a bit of the text, and get the clear impression that onDestroy is always called. Look at "Table 1": [onDestroy] Called before the activity is destroyed. This is the final call that the activity will receive.Sheave
Yes if you don't look at the isKillable after part of the table this is true. If you feel this is an error in the documentation file that as a bug under b.android.com most of the times such a small documentation problem gets a person assigned within a day and it will be corrected the next time the documentation gets updated.Caloric
So I see that onDestroy is not guaranteed to be called. So what code should go in onDestroy then?Shaper
@AdamJohns This method is usually implemented to free resources like threads that are associated with an activity. Or can be use to remove references to this activity to prevent memory leaks. In case the application is killed and onDestroy() isn't called - you don't care as it's gone anyway..Towhead
E
6

In most phones when the back button is pressed there are called twice onStop() and onDestroy() methods, but if it isn't your case, you can create a button to invoke the finish(); method.

Egest answered 15/5, 2013 at 2:50 Comment(1)
No, this feature is usually is turned on at Developer Options - "Don't keep activities" checkbox.Rosy

© 2022 - 2024 — McMap. All rights reserved.