Search code examples
androidondestroy

Activity OnDestroy never called?


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?


Solution

  • 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.