Debugging android activity stack

I’ve been playing with some Android code lately. A friend, Zdenko Zvada is trying to start up a climber’s guide application called Craggie, now in very early beta stage, so I’m helping him and training my programming muscle.

To my surprise the ADT (Eclipse based Android Development Tools) debugging tools don’t support the viewing of current Activity stack, but luckily the adb command line tool does.

adb shell dumpsys activity activities

Invaluable for debugging navigation issues, but the output contains quite a lot of other info so here’s a quick python script to skip to important parts:

#!/usr/bin/python
import subprocess

proc = subprocess.Popen(['adb', 'shell', 'dumpsys', 'activity', 'activities'],stdout=subprocess.PIPE)
skipLine = True
for line in proc.stdout:
   if 'Running activities (most recent first):' in line:
      skipLine = False
   if ('Recent tasks:' in line) and (not skipLine):
      skipLine = True
   if not skipLine: 
      print line,

And this is the example output:

  Running activities (most recent first):
    TaskRecord{42cc9c88 #57 A com.android.email U 0}
      Run #5: ActivityRecord{42ed5980 com.android.email/.activity.MessageCompose}
      Run #4: ActivityRecord{42c5ef70 com.android.email/.activity.MessageListXL}
    TaskRecord{4212e138 #2 A com.sec.android.app.launcher U 0}
      Run #3: ActivityRecord{4212c568 com.sec.android.app.launcher/com.android.launcher2.Launcher}
    TaskRecord{42cb9998 #56 A com.android.chrome U 0}
      Run #2: ActivityRecord{41b61f30 com.android.chrome/com.google.android.apps.chrome.Main}
    TaskRecord{42ef3570 #55 A cc.dict.dictcc U 0}
      Run #1: ActivityRecord{42997698 cc.dict.dictcc/org.appcelerator.titanium.TiActivity}
      Run #0: ActivityRecord{42bfdd30 cc.dict.dictcc/.DictCcActivity}
 
  mResumedActivity: ActivityRecord{42ed5980 com.android.email/.activity.MessageCompose}
  mFocusedActivity: ActivityRecord{42ed5980 com.android.email/.activity.MessageCompose}
  mLastPausedActivity: ActivityRecord{42c5ef70 com.android.email/.activity.MessageListXL}
  mSleepTimeout: false
  mDismissKeyguardOnNextActivity: false