How to find back stack activities in an android application?
Asked Answered
T

8

21

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??

Trial answered 3/11, 2011 at 9:24 Comment(0)
M
6

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();
}
Mcgray answered 3/11, 2011 at 9:32 Comment(4)
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.Wallacewallach
Also this needs GET_TASK permissionHaught
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 reasonsHaught
@for3st Does it return the info about the current app?Ashbey
L
4

You can use "adb shell dumpsys activity activities" command

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

Lithopone answered 8/1, 2015 at 13:25 Comment(1)
I got the list of activities in the stack now how could I can kill? let suppose the 3rd activity in the stackSac
H
1

As getRunningTasks(..) has been deprecated or if you don't want to add special permissions to your app here is an alternative solution if all activities are yours: you can mark an identifier (not the activity itself is it may be in need to be garbage collected) in a singleton Stack, LinkedList or LinkedHashSet: in onPause() add the identifier to the stack and remove it in onResume().

You can check the contents of that stacks to know if there is any activity and the sequence they have been created.

You can clean up your code and forget to do that manually if all your activities derive from a common base activity for your app.

Hardeman answered 20/1, 2015 at 12:12 Comment(0)
G
1

I recommend you to utilize startActivityForResult instead of startActivity, then in order to retrieve the origin/source activity- call getCallingActivity().getClassName().

How will A know it came from home? Use getIntent() its

getAction() -> android.intent.action.MAIN

and

getCategories() -> android.intent.category.LAUNCHER

Glockenspiel answered 30/8, 2015 at 1:30 Comment(0)
A
1

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.

Ashbey answered 27/2, 2016 at 18:8 Comment(0)
A
0

I'm not sure i get it well...You want to go back to the previous activity ? If so, finish the current activity you'll get back to the previous one.

Allanadale answered 3/11, 2011 at 9:26 Comment(2)
i want to find that at activity E, i've navigated from A,B,C,D something like thisTrial
Okay then, Rajdeep Dua has the answer. :PAllanadale
M
0

actually, you can get the task id from the runningtaskinfo using getRunningTasks and then, get the recenttaskinfo using getRecentTasks by compare the task id. now you can restart that activity using startactivity, with the baseintent in the recenttaskinfo.

Mark answered 29/3, 2012 at 7:34 Comment(0)
J
0

The most obvious answer would be keeping a reference somewhere safe like in a utility class as a static variables in certain order, like in a list, but this can be dangerous as it can lead to a memory leak, so make sure you manually handle it and dispose them when needed.

Jacobsohn answered 21/12, 2022 at 1:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.