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.