Thursday, January 8, 2015

Find back stack activities in an android application

An application usually contains multiple activities. A task is a collection of activities that users interact with when performing a certain job. The activities are arranged in a stack (the back stack), in the order in which each activity is opened. The device Home screen (Launcher Application) is the starting place for most tasks.

Finding list of activities in back stack can be done two ways.
1. Using ADB command
2. Using ActivityManager.

1. Using ADB command:
 An ADB (Android Debug Bridge) is a very useful tool for debugging android application. List some useful commands as follows.

1. adb shell dumpsys activity activities  -> displays list of activities in back stack

2. adb shell dumpsys activity process  -> displays list process in back stack

3. adb shell dumpsys activity intents  -> displays list of pending intents in back stack

4. adb shell dumpsys activity broadcast  -> displays list of broadcast in back stack

5. adb shell dumpsys activity services  -> displays list of services running in back stack

Note: Use grep command for filtering.

2. Using ActivityManager
 Use following line of code to get list of activities in stack.

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(); }