149

I just started developing a simple Android application while I'm still learning the platform.

I'm using Eclipse IDE with the ADT plugin 0.9.6.

I need to know if it's possible to view the Activity stack that is associated with a Task?

Is there any way through the DDMS tool or through any other technique?

Essentially what I need is to be able to see the stack activity of a task to make sure that the application behaves as expected.

I know that it's possible to control the task behavior in some extent through the use of flags in the Intent object and through some attributes of the <activity> element.

However it would be nice to have a sort of tool - especially in debug mode or so - that would allow developers to see the Activity stack straight forward.

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Mic
  • 1,491
  • 2
  • 10
  • 3
  • If you are using Android Studio, I have posted a solution [here][1]. [1]: http://stackoverflow.com/a/22392616/1798991 – Nebu Mar 13 '14 at 22:52

9 Answers9

170

From the command line, you can use: 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. There is also a lot of stuff printed after that, so you may need to scroll up a bit to find what you want.

Here is an example of its output (the exact contents varies across platform versions), showing the top task being contacts with two activities, and behind that the launcher with one activity:

Activities in Current Activity Manager State:
  * TaskRecord{44d07218 #4 A android.task.contacts}
    clearOnBackground=true numActivities=2 rootWasReset=true
    affinity=android.task.contacts
    intent={act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10600000 cmp=com.android.contacts/.DialtactsActivity bnds=[125,640][235,758]}
    origActivity=com.android.contacts/.DialtactsContactsEntryActivity
    realActivity=com.android.contacts/.DialtactsActivity
    lastActiveTime=288203177 (inactive for 14s)
    * Hist #8: HistoryRecord{44b87a30 com.android.contacts/.ViewContactActivity}
        packageName=com.android.contacts processName=android.process.acore
        launchedFromUid=10004 app=ProcessRecord{44c4f348 1168:android.process.acore/10004}
        Intent { act=android.intent.action.VIEW dat=content://com.android.contacts/contacts/lookup/144i148.144i461a29500afc8eeb/1927 cmp=com.android.contacts/.ViewContactActivity }
        frontOfTask=false task=TaskRecord{44d07218 #4 A android.task.contacts}
        taskAffinity=android.task.contacts
        realActivity=com.android.contacts/.ViewContactActivity
        base=/system/app/Contacts.apk/system/app/Contacts.apk data=/data/data/com.android.contacts
        labelRes=0x7f090012 icon=0x7f02006b theme=0x7f0e0004
        stateNotNeeded=false componentSpecified=false isHomeActivity=false
        configuration={ scale=1.0 imsi=310/4 loc=en_US touch=3 keys=2/1/2 nav=2/2 orien=1 layout=34}
        resultTo=HistoryRecord{44d174d0 com.android.contacts/.DialtactsContactsEntryActivity} resultWho=favorites resultCode=2
        launchFailed=false haveState=false icicle=null
        state=RESUMED stopped=false delayedResume=false finishing=false
        keysPaused=false inHistory=true persistent=false launchMode=0
        fullscreen=true visible=true frozenBeforeDestroy=false thumbnailNeeded=false idle=true
        waitingVisible=false nowVisible=true
    * Hist #7: HistoryRecord{44d174d0 com.android.contacts/.DialtactsContactsEntryActivity}
        packageName=com.android.contacts processName=android.process.acore
        launchedFromUid=10004 app=ProcessRecord{44c4f348 1168:android.process.acore/10004}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.android.contacts/.DialtactsContactsEntryActivity bnds=[125,640][235,758] }
        frontOfTask=true task=TaskRecord{44d07218 #4 A android.task.contacts}
        taskAffinity=android.task.contacts
        realActivity=com.android.contacts/.DialtactsActivity
        base=/system/app/Contacts.apk/system/app/Contacts.apk data=/data/data/com.android.contacts
        labelRes=0x7f090007 icon=0x7f02006b theme=0x7f0e0000
        stateNotNeeded=false componentSpecified=true isHomeActivity=false
        configuration={ scale=1.0 imsi=310/4 loc=en_US touch=3 keys=2/1/2 nav=2/2 orien=1 layout=34}
        launchFailed=false haveState=true icicle=Bundle[mParcelledData.dataSize=4196]
        state=STOPPED stopped=true delayedResume=false finishing=false
        keysPaused=false inHistory=true persistent=false launchMode=2
        fullscreen=true visible=false frozenBeforeDestroy=false thumbnailNeeded=false idle=true
  * TaskRecord{44c4ee90 #2 A com.android.launcher}
    clearOnBackground=true numActivities=1 rootWasReset=true
    affinity=com.android.launcher
    intent={act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10600000 cmp=com.android.launcher/.Launcher}
    realActivity=com.android.launcher/.Launcher
    lastActiveTime=214734838 (inactive for 73483s)
    * Hist #6: HistoryRecord{44c4d988 com.android.launcher/.Launcher}
        packageName=com.android.launcher processName=android.process.acore
        launchedFromUid=0 app=ProcessRecord{44c4f348 1168:android.process.acore/10004}
        Intent { act=android.intent.action.MAIN cat=[android.intent.category.HOME] flg=0x10000000 cmp=com.android.launcher/.Launcher }
        frontOfTask=true task=TaskRecord{44c4ee90 #2 A com.android.launcher}
        taskAffinity=com.android.launcher
        realActivity=com.android.launcher/.Launcher
        base=/system/app/Launcher.apk/system/app/Launcher.apk data=/data/data/com.android.launcher
        labelRes=0x7f0a0000 icon=0x7f020015 theme=0x103005f
        stateNotNeeded=true componentSpecified=false isHomeActivity=true
        configuration={ scale=1.0 imsi=310/4 loc=en_US touch=3 keys=2/1/2 nav=2/2 orien=1 layout=34}
        launchFailed=false haveState=true icicle=Bundle[mParcelledData.dataSize=5964]
        state=STOPPED stopped=true delayedResume=false finishing=false
        keysPaused=false inHistory=true persistent=false launchMode=2
        fullscreen=true visible=false frozenBeforeDestroy=false thumbnailNeeded=false idle=true
Nolan Amy
  • 10,785
  • 5
  • 34
  • 45
hackbod
  • 90,665
  • 16
  • 140
  • 154
  • 1
    is there a nice way to show all of the tasks&activities stack of the current app via logcat? do we really need to parse the adb command ? – android developer Feb 13 '12 at 06:36
  • 92
    In addition, if you only want to see the activities' name in the stack you can do this: `adb shell` **** `dumpsys activity | grep -i run`. – Surya Wijaya Madjid Feb 14 '12 at 19:34
  • 6
    Great answer! I've written a [convenience script](https://github.com/sschuberth/dev-scripts/blob/master/android/show_tasks.sh) which filters the output to get the tasks / activities for a given package. – sschuberth Jul 20 '12 at 09:49
  • 7
    in addition to `adb shell dmpsys activity` you can get for each list for example `adb shell dmpsys activity activities` for ACTIVITY MANAGER ACTIVITIES that contains main stack, Running activities and Recent tasks.`dumpsys activity intents` for pending intents; `dumpsys activity broadcasts` for broadcast state; `dumpsys activity providers` for content proviers; `dumpsys activity services` for services; `dumpsys activity processes` for running processes. – Fredrick Gauss Sep 14 '13 at 08:09
  • any idea how to get the ActivityRecord details programatically ? – AndroidGuy May 11 '15 at 07:36
  • 2
    @SuryaWijayaMadjid's command can be done in one line: `adb shell dumpsys activity | grep -i run`, or `adb shell dumpsys activity activities | grep -i run` for slightly cleaner output. – vaughandroid Sep 28 '18 at 07:38
67

You can use the following command in your command line to see the tasks and backstacks in the system:

adb shell dumpsys activity activities | sed -En -e '/Stack #/p' -e '/Running activities/,/Run #0/p'

Or you can try TaskLogger, a simple tool I created which can monitor all activities and tasks in your App, and output them in Logcat in real-time.

frankelot
  • 13,666
  • 16
  • 54
  • 89
Gerald.K
  • 671
  • 5
  • 4
  • +1 .... I have tried your TaskLogger it is good tool and helps me a lot, but prints avalanche of unwanted logs. – ThinkDeep Jun 14 '16 at 12:15
43

If you want to inspect the task stack of a specific package, the following command will do:

adb shell dumpsys activity activities | grep PACKAGE_NAME | grep Hist
neevek
  • 11,760
  • 8
  • 55
  • 73
35

I know this is an old question but, this functionality is now baked into Android Studio:

android studio screenshot

Then in the resulting text file, search for ACTIVITY (all caps):

android studio text file screenshot

tir38
  • 9,810
  • 10
  • 64
  • 107
  • 22
    I think this option no longer exist since the new Android Profiler window in Android Studio 3.0 replaces the Android Monitor tools. – 林果皞 Oct 25 '17 at 06:47
  • 6
    I created issue for this missing function: https://issuetracker.google.com/issues/77944626 so please vote for it. Thanks – mtrakal Apr 12 '18 at 13:22
14

For the list of recent tasks

adb shell dumpsys activity recents

For the list of Services running

adb shell dumpsys activity services

For the list of current Content Providers

adb shell dumpsys activity providers

For the list of Broadcast state

adb shell dumpsys activity broadcasts

For the list of Pending Intents

adb shell dumpsys activity intents

For the list of permissions

adb shell dumpsys activity permissions
Prasad
  • 3,462
  • 1
  • 23
  • 28
  • If you like more GUI-sh way you can use `AdbCommander` plugin and add those commands in `macros` tab – prot0n Dec 12 '17 at 14:21
12

I always check this part of long dump messages..

  Running activities (most recent first):
TaskRecord{4307f828 #56 A com.demo.proj U 0}
  Run #4: ActivityRecord{425a6838 com.demo.proj/com.demo.proj.Activity2}
  Run #3: ActivityRecord{427dc860 com.demo.proj/com.demo.proj.Activity1}
  Run #2: ActivityRecord{420cba18 com.demo.proj/com.demo.proj.MainActivity}
TaskRecord{430341d0 #2 A com.lge.launcher2 U 0}
  Run #1: ActivityRecord{41e0af68 com.lge.launcher2/.Launcher}
TaskRecord{44e26ce0 #18 A com.lge.appbox.client U 0}
  Run #0: ActivityRecord{41e9dbe8 com.lge.appbox.client/.AppBoxClient}

Note: Run #4 is the activity that you see now on the screen. :)

cmcromance
  • 2,289
  • 1
  • 25
  • 26
  • 2
    What's "long dump messages"? – Marian Paździoch Jul 18 '14 at 07:35
  • 2
    @MarianPaździoch "adb shell dumpsys activity" shows us too LONG messages. This messages, above, is a bit of them. By the way, I got a tip can avoid this. Run this, "adb shell dumpsys activity activities" You can see shorter message and can read more easily about the activity stack. :) – cmcromance Jul 18 '14 at 07:50
  • 1
    ...and if that list is still too long, open the Recent Apps list and swipe away some tasks. – Barry Fruitman Mar 29 '15 at 00:13
11

There is a plugin for that now:

https://plugins.jetbrains.com/plugin/12293-activity-stack-view

enter image description here

CHAN
  • 1,518
  • 18
  • 16
  • 1
    Seems broken on Android Studio Electric Eel `java.lang.AbstractMethodError: Receiver class com.cheng.plugins.adb.ActivityStackCommand$1 does not define or inherit an implementation of the resolved method 'abstract void processNewLines(java.util.List)' of abstract class org.jetbrains.android.util.AndroidOutputReceiver.` – VIN Feb 17 '23 at 17:15
  • 1
    I concur with what @VIN said. Not working in Electric Eel. Best I could figure out is running this command `adb shell dumpsys activity activities | grep YOUR_PACKAGE_NAME | grep Hist` – Ramakrishna Joshi May 17 '23 at 16:12
10

You can use the tool hierarchyviewer.bat. It is part of the android SDK. It only works with emulator though. But it is much more confortable and clearer.

Edit: I just found the Hierarchy Viewer within Eclipse! And it works with real devices as well. Just open the perspective Windows->Open Perspective-> Hierarchy View In the list you can see the all the 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.

Edit: The Hierarchy Viewer will work only with developer devices. Production devices cannot do it for security reasons. For more information please take a look at following answer

Community
  • 1
  • 1
Xazen
  • 822
  • 2
  • 12
  • 26
  • 4
    Hierarchy Viewer is for viewing an activity's View hierarchy. The question was about the [task/activity stack](http://developer.android.com/guide/components/tasks-and-back-stack.html). – Jeremy Logan Mar 25 '14 at 17:06
1

Solution: 'adb shell dumpsys activity' does not work with TabActivity. When each tab item selected, corresponding activity will be launch. But when use 'adb shell dumpsys activity' It always return 'main' activity:

public class main extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.e("xyz", "start main...............");
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, widgets.class);
        spec = tabHost.newTabSpec("Widgets").setIndicator("Widgets", res.getDrawable(R.drawable.tab1)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, layouts.class);
        spec = tabHost.newTabSpec("Layouts").setIndicator("Layouts",res.getDrawable(R.drawable.tab2)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, composite1.class);
        spec = tabHost.newTabSpec("Composite").setIndicator("Composite",res.getDrawable(R.drawable.tab3)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, imageMedia.class);
        spec = tabHost.newTabSpec("Image_Media").setIndicator("Image&Media",res.getDrawable(R.drawable.tab4)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, timeDate.class);
        spec = tabHost.newTabSpec("Time_Date").setIndicator("Time&Date",res.getDrawable(R.drawable.tab5)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, transitions.class);
        spec = tabHost.newTabSpec("Transitions").setIndicator("Transitions",res.getDrawable(R.drawable.tab6)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, advanced.class);
        spec = tabHost.newTabSpec("Advanced").setIndicator("Advanced",res.getDrawable(R.drawable.tab7)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, others.class);
        spec = tabHost.newTabSpec("Others").setIndicator("Others",res.getDrawable(R.drawable.tab8)).setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Dynamic.class);
        spec = tabHost.newTabSpec("Dynamic").setIndicator("Dynamic",res.getDrawable(R.drawable.tab2)).setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);

    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Pyraman
  • 49
  • 5