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:

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 U 0}
      Run #5: ActivityRecord{42ed5980}
      Run #4: ActivityRecord{42c5ef70}
    TaskRecord{4212e138 #2 A U 0}
      Run #3: ActivityRecord{4212c568}
    TaskRecord{42cb9998 #56 A U 0}
      Run #2: ActivityRecord{41b61f30}
    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}
  mFocusedActivity: ActivityRecord{42ed5980}
  mLastPausedActivity: ActivityRecord{42c5ef70}
  mSleepTimeout: false
  mDismissKeyguardOnNextActivity: false