How to Check If an Activity Is the Last One in the Activity Stack for an Application

How to check if an activity is the last one in the activity stack for an application?

UPDATE (Jul 2015):

Since getRunningTasks() get deprecated, from API 21 it's better to follow raukodraug answer or Ed Burnette one (I would prefer second one).



There's possibility to check current tasks and their stack using ActivityManager.

So, to determine if an activity is the last one:

  • request android.permission.GET_TASKS permissions in the manifest.
  • Use the following code:

    ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );

    List<ActivityManager.RunningTaskInfo> taskList = mngr.getRunningTasks(10);

    if(taskList.get(0).numActivities == 1 &&
    taskList.get(0).topActivity.getClassName().equals(this.getClass().getName())) {
    Log.i(TAG, "This is last activity in the stack");
    }

Please note, that above code will be valid only if You have single task. If there's possibility that number of tasks will exist for Your application - You'll need to check other taskList elements. Read more about tasks Tasks and Back Stack


how to check if activity is still in the stack?

Look at the ActivityManager API

To get an instance of the ActivityManager use this code:

ActivityManager mngr = (ActivityManager) getSystemService( ACTIVITY_SERVICE );

Android: how do I check if activity is running?

You can use a static variable within the activity.

class MyActivity extends Activity {
static boolean active = false;

@Override
public void onStart() {
super.onStart();
active = true;
}

@Override
public void onStop() {
super.onStop();
active = false;
}
}

The only gotcha is that if you use it in two activities that link to each other then onStop on the first is sometimes called after onStart in second. So both might be true briefly.

Depending on what you are trying to do (update the current activity from a service?). You could just register a static listener in the service in your activity onStart method then the correct listener will be available when your service wants to update the UI.

How to check if activity is in foreground or in visible background?

This is what is recommended as the right solution:

The right solution (credits go to Dan, CommonsWare and NeTeInStEiN)
Track visibility of your application by yourself using
Activity.onPause, Activity.onResume methods. Store "visibility" status
in some other class. Good choices are your own implementation of the
Application or a Service (there are also a few variations of this
solution if you'd like to check activity visibility from the service).

Example
Implement custom Application class (note the isActivityVisible() static method):

public class MyApplication extends Application {

public static boolean isActivityVisible() {
return activityVisible;
}

public static void activityResumed() {
activityVisible = true;
}

public static void activityPaused() {
activityVisible = false;
}

private static boolean activityVisible;
}

Register your application class in AndroidManifest.xml:

<application
android:name="your.app.package.MyApplication"
android:icon="@drawable/icon"
android:label="@string/app_name" >

Add onPause and onResume to every Activity in the project (you may
create a common ancestor for your Activities if you'd like to, but if
your activity is already extended from MapActivity/ListActivity etc.
you still need to write the following by hand):

@Override
protected void onResume() {
super.onResume();
MyApplication.activityResumed();
}

@Override
protected void onPause() {
super.onPause();
MyApplication.activityPaused();
}

In your finish() method, you want to use isActivityVisible() to check if the activity is visible or not. There you can also check if the user has selected an option or not. Continue when both conditions are met.

The source also mentions two wrong solutions...so avoid doing that.

Source: stackoverflow

How to get a list of the activity history stack?


So is there a way to do that using a function from the SDK?

No, sorry.

Would it be possible to listen to "start/finish activity" events with some kind of receiver to a specific app (mine) and keep track of the stack history?

You can use registerActivityLifecycleCallbacks() on Application, if you are only going to support API Level 14 and higher (a.k.a., Android 4.0+).

Check whether activity is active

There might be an easier way I can't think of but one way is to implement it yourself. On onResume() you set a member variable mIsRunning to true and on onPause() back to false. Using this boolean you should know not to call alert.show() on your callback.

How to know activity has been finished?

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.



Related Topics



Leave a reply



Submit