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?

Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
UMAR-MOBITSOLUTIONS
  • 77,236
  • 95
  • 209
  • 278

3 Answers3

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.

Mehdi Charife
  • 722
  • 1
  • 7
  • 22
Vladimir Ivanov
  • 42,730
  • 18
  • 77
  • 103
  • 4
    onDestroy is not only called when the system is low on resources. It is also called when an `Activity` is not reachable anymore – Buddy Aug 01 '15 at 11:29
  • 2
    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. – dodgy_coder Nov 11 '15 at 13:46
  • 1
    So how to know when the user is killing the app? – Gerrard Oct 30 '19 at 23:27
  • 6
    Why is this 60 upvotes and correct answer when it's completely wrong about when onDestroy is called? – Trevor Oct 16 '20 at 20:11
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.

Janusz
  • 187,060
  • 113
  • 301
  • 369
  • 7
    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()." http://developer.android.com/guide/topics/fundamentals/activities.html#Lifecycle – Ted Oct 11 '11 at 22:48
  • 1
    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. – Janusz Oct 12 '11 at 06:20
  • 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. – Ted Oct 12 '11 at 08:35
  • 1
    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. – Janusz Oct 12 '11 at 10:03
  • 1
    So I see that onDestroy is not guaranteed to be called. So what code should go in onDestroy then? – Adam Johns Dec 11 '14 at 04:06
  • 1
    @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.. – Alexandre G Mar 08 '17 at 01:03
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.

Jordi Tamargo
  • 61
  • 1
  • 2