View the Task's Activity Stack

View activity stack in Android

To display the activity stack, run the following command :

adb shell dumpsys activity activities

How to see the activity stack in debug?

1-You can use Hierarchy Viewer within Eclipse.You can see all connected devices and emulators and the activity stack. And in addition in the tree view you can see much more information about the view itself:

Sample Image

2-From the command line, you can use this: adb shell dumpsys activity.This asks the activity manager to print a dump of its current state. The first part of that is the complete activity history, organized by task.

You can see more details here.

Android | Print activity task stack/s in LogCat

What I am trying to do isn't possible out of the box using APIs within the SDK as of now. More info can be found in this link:

How to get a list of my app's tasks and the stack of their Activities?

My Solution:

I had to add logging in base class of my app, printing the activity's name with its task ID to debug the problem I'm facing. Here's the code of my base activity class:

public abstract class BaseAppActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("TESTING", "CREATED: " + getClass().getSimpleName() + " -- TASK ID: " + getTaskId());
}

@Override
protected void onDestroy() {
super.onDestroy();
Log.i("TESTING", "DESTROYED: " + getClass().getSimpleName() + " -- TASK ID: " + getTaskId());
}
}

How to get a list of my app's tasks and the stack of their Activities?

How can I get a list of all the Tasks for my current application

There is no such concept. Tasks can contain activities from an application. To drawn an analogy, no Web site owns the back stack of a browser tab, though a Web site's pages may be in the back stack of a browser tab.

then get the Activities on those tasks' stacks?

That is not possible, as the activities are not necessarily all from your process.

The ActivityManager class lets me query task info based on what's recent or what's running but how do I query task info just for my current app?

You don't, strictly speaking.

You can iterate over the running tasks, and you can find those where one of your activities is the baseActivity or the topActivity based on those ComponentName values.

Or, your activities know their task ID, obtained via getTaskId(). If you need to, you can find those tasks in the running tasks, matching up the ID values, to find out task-level details about the activity's task.

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




Related Topics



Leave a reply



Submit