Search code examples
androidandroid-lifecycledestroy

Android beginner: onDestroy


Shall I place commands before or after super.onDestroy() when overwriting an activity's ondestroy?

protected void onDestroy() {

    //option 1: callback before or ...

    super.onDestroy();

    //option 2: callback after super.onDestroy();
}

(Now I fear: If super.onDestroy is too fast, it will never arrive in option 2.)


Solution

  • This is what happens when you call super.onDestroy();

    Android 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 Dialog dialog = mManagedDialogs.valueAt(i);
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
            }
        }
    
        // also dismiss search dialog if showing
        // TODO more generic than just this manager
        SearchManager searchManager = 
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchManager.stopSearch();
    
        // close any cursors we are managing.
        int numCursors = mManagedCursors.size();
        for (int i = 0; i < numCursors; i++) {
            ManagedCursor c = mManagedCursors.get(i);
            if (c != null) {
                c.mCursor.close();
            }
        }
    }
    

    Essentially this means that it does not matter if you call it before or after your code.