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.ActivityLifecycleCallbacks.html
This will help you to add to a stack of your own classes, and see the full state as you wish.
![azzits](https://i.stack.imgur.com/t6auc.jpg?s=256&g=1)
Author by
azzits
Updated on June 12, 2020Comments
-
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 over 12 yearsi want to find that at activity E, i've navigated from A,B,C,D something like this
-
Quentin DOMMERC over 12 yearsOkay then, Rajdeep Dua has the answer. :P
-
Michael Smith over 12 yearsThis 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 about 10 yearsAlso this needs GET_TASK permission
-
Anas Reza almost 9 yearsI got the list of activities in the stack now how could I can kill? let suppose the 3rd activity in the stack
-
Patrick over 8 yearsAs 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 over 8 years@for3st Does it return the info about the current app?