View the Android activity stack

Have you ever been developing an Android app, and wanted to find out what activity you were looking at on the screen without spelunking through mountains of source code? Well luckily, there is a quick way to view the Android activity stack.

(Please note that these instructions are for use on a Mac. The corresponding Windows way of doing things is undoubtedly similar.)

If you have ADB installed and running on the command line, then go into Terminal and enter the following command:

adb shell dumpsys activity | grep -i run

Upon doing this, you should see a listing that looks something like this:

mbp:~ user$ adb shell dumpsys activity | grep -i run
    Running activities (most recent first):
        Run #5: ActivityRecord{3d31e5ec u0 com.yourcompany.yourapp/.ui.CustomerDetailActivity t211}
        Run #4: ActivityRecord{18b02af2 u0 com.yourcompany.yourapp/.ui.CustomerListActivity t211}
        Run #3: ActivityRecord{517ab43 u0 com.android.settings/.Settings t180}
        Run #2: ActivityRecord{2e389c3c u0 com.android.chrome/com.google.android.apps.chrome.document.DocumentActivity t179}
        Run #1: ActivityRecord{8583e3d u0 com.google.android.googlequicksearchbox/com.google.android.sidekick.main.optin.NewOptInActivity t153}
        Run #0: ActivityRecord{14e94b15 u0 com.google.android.googlequicksearchbox/com.google.android.apps.gsa.legacyui.VelvetActivity t153}
(more stuff after, not important)

If you look at the top of that list, you will see that the CustomerDetailActivity class is at the top of the activity stack.

Keep in mind that you may need to get ADB set up on your Mac, the easiest way is to install the tools through brew, and here are the Terminal commands necessary to do this:

1. Install homebrew:

ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

2. Install ADB:

brew install android-platform-tools

3. Test ADB:

adb devices

That last command above will just verify that you have a device hooked into your computer. This also appears to work with the Android emulator provided by Android Studio, but if you are using Genymotion, it should work fine with that one as well.

BTW, Happy 50th Birthday to Star Trek, which debuted on American television on this date way back in 1966.

P.S. Credit where credit is due: View activity stack in Android – Stack Overflow and Installing ADB on MAC OS X – Stack Overflow were obviously key.

Leave a Reply