Activity OnDestroy never called?

UMAR-MOBITSOLUTIONS picture UMAR-MOBITSOLUTIONS · Dec 15, 2010 · Viewed 75.6k times · Source

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?

Answer

Vladimir Ivanov picture Vladimir Ivanov · Dec 15, 2010

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.