5

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

Blaisorblade
  • 6,438
  • 1
  • 43
  • 76
ledy
  • 1,527
  • 6
  • 22
  • 33
  • i don't see what could prevent you from reaching 2 – njzk2 Oct 12 '12 at 12:37
  • This question is a dup of http://stackoverflow.com/q/18821481/53974, and that one actually has a well-researched answer. http://stackoverflow.com/a/18874519/53974 – Blaisorblade Aug 27 '16 at 12:42

6 Answers6

7

Anything that might be related to using the activity resources should be before the call to super.onDestroy(). The code after it will b reached, but might cause problems if it needs those resources.

MByD
  • 135,866
  • 28
  • 264
  • 277
  • why doesn't the android developer manual not recommend this anywhere :-( i seeked the documentation for hours... – ledy Oct 12 '12 at 12:38
4

Place your code after the super.onDestroy(); eg:

protected void onDestroy() {
    super.onDestroy();

    // Put code here.

}

Your code will finish executing when overriding the method.

Luke Taylor
  • 9,481
  • 13
  • 41
  • 73
  • 1
    i am going to call testActivity.Instance.InfoThatIAmComingBackToTheMainActivityNow() – ledy Oct 12 '12 at 12:54
3

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.

ian.shaun.thomas
  • 3,468
  • 25
  • 40
  • does it also mean i don't need to call "dismiss()" for the onDestroy of the activity? i think i remember that there were logs of leaking them , no? in which cases do the leaks occur? – android developer Nov 04 '13 at 10:25
  • 1
    Dismiss is only for Dialogs and unrelated to this question as a Dialog has a different life cycle than an activity. If you are having dialog issues you should start a new question so your isssue can be better addressed. – ian.shaun.thomas Nov 04 '13 at 14:48
  • but it is related to it, as it asks about where to put commands into it, and you've shown its code. the code seem to have dismissing dialogs mechanism – android developer Nov 04 '13 at 15:33
  • The onDestroy only dismisses managed dialogs. If you arbitrarily created a dialog, this method does not know about it. – ian.shaun.thomas Nov 04 '13 at 19:39
1

Calling super.onDestroy will not interrupt calling thread or something like this. Your code will be executed no matter where you place it, before or after super.onDestroy.

super.onDestroy will only free resources that might be referenced for this activity by framework (such as system dialogs and managed cursors)

I suggest you check this link for more details

http://developer.android.com/reference/android/app/Activity.html#onDestroy()

Igor B.
  • 1,134
  • 9
  • 6
0

It depends. If you want the your actions tobe applied after the super function, you should place your function after the super. I guess you have to understand the usage of super first. For example, take a look at this question

Community
  • 1
  • 1
C.d.
  • 9,932
  • 6
  • 41
  • 51
0

It will arrive in option 2. onDestroy() doesn't actually destroy the object. Your instance is still alive after the superclass's onDestroy() runs and returns.

Edit: this is what onDestroy does in order:

  • Dismiss any dialogs the activity was managing.
  • Close any cursors the activity was managing.
  • Close any open search dialog
Robby Smet
  • 4,649
  • 8
  • 61
  • 104
  • i.e. if the device was fast enough, a reference or cursor could be gone even before it arrives with option 2? – ledy Oct 12 '12 at 12:50