Android - Will the value of isFinishing() remain true after an Activity has been destroyed?
Asked Answered
F

2

7

Apologies if this question sounds incredibly basic. I have an Activity that has an asynchronous network callback. The callback can execute after a user leaves the Activity.

As a check I'd like to use isFinishing() (I cannot use isDestroyed() as my min API level is 16 and not 17 which isDestroyed() requires).

Can I use isFinishing() in the callback to ensure that my logic executes only when the Activity is not destroyed?

More specifically does isFinishing() return true for an Activity that is destroyed by calling finish() even after onDestroy() is called ?

I also had a look at the source code. Here is isFinishing():

    public boolean isFinishing() {
        return mFinished;
    }

And here is finish() where the variable is set to true:

   /**
     * Finishes the current activity and specifies whether to remove the task associated with this
     * activity.
     */
    private void finish(boolean finishTask) {
        if (mParent == null) {
            int resultCode;
            Intent resultData;
            synchronized (this) {
                resultCode = mResultCode;
                resultData = mResultData;
            }
            if (false) Log.v(TAG, "Finishing self: token=" + mToken);
            try {
                if (resultData != null) {
                    resultData.prepareToLeaveProcess();
                }
                if (ActivityManagerNative.getDefault()
                        .finishActivity(mToken, resultCode, resultData, finishTask)) {
                    mFinished = true;
                }
            } catch (RemoteException e) {
                // Empty
            }
        } else {
            mParent.finishFromChild(this);
        }
    }

    /**
     * Call this when your activity is done and should be closed.  The
     * ActivityResult is propagated back to whoever launched you via
     * onActivityResult().
     */
    public void finish() {
        finish(false);
    }

I also had a look at Understanding of isFinishing() but I can't seem to derive an answer to this particular question.

Fernandina answered 29/8, 2016 at 11:12 Comment(3)
same as the #5227571Lyford
I referenced that in the post, but I can't seem to derive an answer to my question. "More specifically does isFinishing() return true for an Activity that is destroyed by calling finish() even after onDestroy() is called ?"Fernandina
Refer to this thread for more details about isFinishing #5227571Latta
D
3

Your question is probably as good an answer as any answer could be since the javadoc of Activity.isFinishing() does not specify the return value for an already-destroyed Activity. However, judging by the source it seems that the not-at-all-confusingly-named mFinished (used by isFinishing()) is never set to false (other than on initialisation) and so once set to true will always stay that value. Having said that, mFinished is package-private so in theory another class could be modifying that value. In practice, I think it is safe to assume that isFinishing() returns true iff an Activity is finishing or has finished finishing!

In other words, isFinishing() == isFinishing() || isDestroyed()

Deraign answered 6/4, 2018 at 9:25 Comment(0)
S
0

I can confirm that I replaced (isFinishing() || isDestroyed) with isFinishing() and got a number of IllegalArgumentExceptions from Glide with the error You cannot start a load for a destroyed activity. Not perfect evidence, but enough to convince me that, at least on some platforms / devices, the two are not the same.

Schooner answered 3/3, 2021 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.