How to know which activities are running in Android

An Android process does not correspond to an Android activity, which means that even if there is a running process corresponding to your application (I’m talking about the processed listed by the “ps” command), you cannot use this information to know how many of your activities are still alive.

OK, so if you cannot use “ps” command, how can you know which activities are running ?

Well, you need to use the “dumpsys activity” command.

To demonstrate this, I’m going to use a sample application:

  • the package name in the AndroidManifest.xml is fr.lk.notes (i.e: package=”fr.lk.notes”)
  • the main activity is named fr.lk.notes.activity.ViewNotes
  • there is a second activity named¬†fr.lk.notes.activity.CreateOrEditNote

Exercice #1: we launch the application and then exit it by clicking on the BACK button

This will invoke ViewNotes.onDestroy(), which means that the main activity will be killed.

Yet, the “ps” command shows that the application process is still running:


C:\android-sdk\platform-tools>adb shell ps
USER PID PPID VSIZE RSS WCHAN PC NAME
root 1 0 404 280 c0274189 08054b16 S /init
[SNIP]
u0_a43 1635 793 216720 31232 ffffffff b802c157 S fr.lk.notes
[SNIP]

Now if we launch the “dumpsys activity” command, and if we look at the “Running activities” section, we can see that there is no reference to any of our activites, which means that they have all been destroyed:


C:\android-sdk\platform-tools>adb shell dumpsys activity
[SNIP]

Running activities (most recent first):

TaskRecord{b3e1dc08 #2 A com.android.launcher U 0}
 Run #0: ActivityRecord{b3e04590 com.android.launcher/com.android.launcher2.Launcher}

[SNIP]

Exercice #2: we launch the application

This puts the main “ViewNotes” activity in a running state and we can see it in the dumpsys output:


[SNIP]

Running activities (most recent first):

TaskRecord{b3eb03a8 #6 A fr.lk.notes U 0}
 Run #1: ActivityRecord{b3c1f2d8 fr.lk.notes/.activity.ViewNotes}
 TaskRecord{b3e1dc08 #2 A com.android.launcher U 0}Run #0: ActivityRecord{b3e04590 com.android.launcher/com.android.launcher2.Launcher

[SNIP]

Exercice #3: we launch the application and navigate to the second activity

This leaves the main activity in a “stopped” state and dumpsys shows both activities:


[SNIP]

Running activities (most recent first):

TaskRecord{b3eb03a8 #6 A fr.lk.notes U 0}
 Run #2: ActivityRecord{b3ee1ce0 fr.lk.notes/.activity.CreateOrEditNote}
 Run #1: ActivityRecord{b3c1f2d8 fr.lk.notes/.activity.ViewNotes}
 TaskRecord{b3e1dc08 #2 A com.android.launcher U 0}Run #0: ActivityRecord{b3e04590 com.android.launcher/com.android.launcher2.Launcher}

[SNIP]

Laurent KUBASKI

About these ads

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: