Android beginner: onDestroy
Asked Answered
S

6

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

Sluggish answered 12/10, 2012 at 12:36 Comment(2)
i don't see what could prevent you from reaching 2Landscape
This question is a dup of https://mcmap.net/q/210970/-what-is-the-correct-order-of-calling-superclass-methods-in-onpause-onstop-and-ondestroy-methods-and-why/53974, and that one actually has a well-researched answer. https://mcmap.net/q/210970/-what-is-the-correct-order-of-calling-superclass-methods-in-onpause-onstop-and-ondestroy-methods-and-whySizzler
A
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.

Appendage answered 12/10, 2012 at 14:2 Comment(4)
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?Crustacean
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.Appendage
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 mechanismCrustacean
The onDestroy only dismisses managed dialogs. If you arbitrarily created a dialog, this method does not know about it.Appendage
M
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.

Mechanotherapy answered 12/10, 2012 at 12:37 Comment(1)
why doesn't the android developer manual not recommend this anywhere :-( i seeked the documentation for hours...Sluggish
B
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.

Bili answered 12/10, 2012 at 12:42 Comment(1)
i am going to call testActivity.Instance.InfoThatIAmComingBackToTheMainActivityNow()Sluggish
A
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.

Appendage answered 12/10, 2012 at 14:2 Comment(4)
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?Crustacean
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.Appendage
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 mechanismCrustacean
The onDestroy only dismisses managed dialogs. If you arbitrarily created a dialog, this method does not know about it.Appendage
D
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()

Dato answered 12/10, 2012 at 12:56 Comment(0)
E
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

Eadith answered 12/10, 2012 at 12:39 Comment(0)
C
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
Canonicals answered 12/10, 2012 at 12:42 Comment(1)
i.e. if the device was fast enough, a reference or cursor could be gone even before it arrives with option 2?Sluggish

© 2022 - 2024 — McMap. All rights reserved.