How to find back stack activities in an android application?

29,622

Solution 1

The code below can be used to extract all the tasks and the top activity within each task in the back stack

ActivityManager m = (ActivityManager) ctx.getSystemService( ctx.ACTIVITY_SERVICE );
List<RunningTaskInfo> runningTaskInfoList =  m.getRunningTasks(10);
Iterator<RunningTaskInfo> itr = runningTaskInfoList.iterator();
while(itr.hasNext()){
    RunningTaskInfo runningTaskInfo = (RunningTaskInfo)itr.next();
    int id = runningTaskInfo.id;
    CharSequence desc= runningTaskInfo.description;
    int numOfActivities = runningTaskInfo.numActivities;
    String topActivity = runningTaskInfo.topActivity.getShortClassName();
}

Solution 2

You can use "adb shell dumpsys activity activities" command

for reference http://www.onsandroid.com/2015/01/find-back-stack-activities-in-android.html

Solution 3

I think you can do it by listening to the changes of the activities, via this API of registerActivityLifecycleCallbacks :

https://developer.android.com/reference/android/app/Application.html#registerActivityLifecycleCallbacks(android.app.Application.ActivityLifecycleCallbacks)

https://developer.android.com/reference/android/app/Application.ActivityLifecycleCallbacks.html

This will help you to add to a stack of your own classes, and see the full state as you wish.

Share:
29,622
azzits
Author by

azzits

Updated on June 12, 2020

Comments

  • azzits
    azzits about 4 years

    I have an application with activities back stack A -> B -> C -> D -> E. Now at activity E, I want to know the back stack activities that I navigated from. How do I find this??

  • azzits
    azzits over 12 years
    i want to find that at activity E, i've navigated from A,B,C,D something like this
  • Quentin DOMMERC
    Quentin DOMMERC over 12 years
    Okay then, Rajdeep Dua has the answer. :P
  • Michael Smith
    Michael Smith over 12 years
    This is not the same as the back stack - the back stack can involve multiple activities within the same task. I don't think there is a way to do what the OP is asking.
  • Patrick
    Patrick about 10 years
    Also this needs GET_TASK permission
  • Anas Reza
    Anas Reza almost 9 years
    I got the list of activities in the stack now how could I can kill? let suppose the 3rd activity in the stack
  • Patrick
    Patrick over 8 years
    As of LOLLIPOP (sdk 21+) this is deprecated and only returns a very small subset of the information provided in former versions beacause of security reasons
  • android developer
    android developer over 8 years
    @for3st Does it return the info about the current app?