Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a list of the activity history stack?

I'm struggling with an issue in my app. I'd like to provide a way to list the history of the previous opened activities.

I think there are two potential solutions but I'm not sure to find a stable way to achieve any of them.

  • Somehow use a function from the SDK. I tried using getPackageManager() and getActivityManager() but I can't find any suitable solution
  • Implement my own history but then I'll have to override startActivity and I don't really want to go down that way as an activity can be started in other multiple ways.

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

Many thanks.

EDIT:

I've got another idea, it's a bit different. 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?

like image 938
Romain Piel Avatar asked Dec 02 '12 22:12

Romain Piel


People also ask

How do I get activity stack?

This example demonstrate about How to get top activity name in activity stack. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.

How do I print an activity stack?

Find the System Information tab in Android Monitor . Then, Activity Manager State . Then, it shall generate you stack of your activities. Look for ACTIVITY(all caps).

What is activity stack?

A task is a collection of activities that users interact with when trying to do something in your app. These activities are arranged in a stack—the back stack—in the order in which each activity is opened. For example, an email app might have one activity to show a list of new messages.

How can I see my previous activity?

Android activities are stored in the activity stack. Going back to a previous activity could mean two things. You opened the new activity from another activity with startActivityForResult. In that case you can just call the finishActivity() function from your code and it'll take you back to the previous activity.


Video Answer


1 Answers

Using ADB commands:

adb shell dumpsys activity activities  # displays list of activities in back stack  adb shell dumpsys activity process  # displays list process in back stack  adb shell dumpsys activity intents  # displays list of pending intents in back stack  adb shell dumpsys activity broadcast  # displays list of broadcast in back stack  adb shell dumpsys activity services  # displays list of services running in back stack 

Using Activity manager:

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(); } 
like image 197
Pradeep Avatar answered Sep 28 '22 16:09

Pradeep