How to know activity has been finished?

I want to check to see if an activity is running or finished. Is there any method through which I can check the status of activity?

I found activity.isFinishing() but I am not sure about it.

Upvotes: 17

Views: 25887

Answers (3)

David
David

Reputation: 1017

I realize this is very old, but for anyone using Kotlin:

We can check within a Fragment:

// FRAGMENT
override fun onPause() {
    super.onPause()
    // Log.v(TAG, "onPause");
    if(requireActivity().isChangingConfigurations) {
         // Config change
    } else {
        if(requireActivity().isFinishing) {
            // deal with activity ending (finish up viewModel etc)
        }
        // deal with rotate or other config changes as required
    }
}

Of course, we can also just do this directly inside an activity (just remove the requireActivity() parts).

Upvotes: 0

live-love
live-love

Reputation: 52366

Call isFinishing in the onPause method before the activity is destroyed:

@Override
protected void onPause() {
    super.onPause();
    if (isFinishing()) {
        // Here  you can be sure the Activity will be destroyed eventually
    }
}

Upvotes: 6

Adil Soomro
Adil Soomro

Reputation: 37729

If you want to perform any step before Activity is going to become invisible.

Their are several choices here.

onDestroy() - for final cleanup.

isFinishing() - right after act.finish() is called it will return true.

onStop() - when the Activity is killed by framework process. (not destroyed)

onPause() - when the Activity is covered by any other Activity

onBackPressed() - capturing the event of hardware Back key triggered by user.

Upvotes: 21

Related Questions