Getting info about your currently running activities

At some point you’ll find yourself needing to get information about the activities running in a certain task.
There’s an easy way of getting a list of running tasks from the ActivityManager service.
You can request a maximum number of tasks running on the phone, and by default, the currently active task is returned first.

Once you have that you can get a ComponentName object by requesting the topActivity from your list.

Here’s an example.

   ActivityManager am = (ActivityManager) this.getSystemService(ACTIVITY_SERVICE);

		// get the info from the currently running task
		List< ActivityManager.RunningTaskInfo > taskInfo = am.getRunningTasks(1);

		Log.d("topActivity", "CURRENT Activity ::"
				+ taskInfo.get(0).topActivity.getClassName());

		ComponentName componentInfo = taskInfo.get(0).topActivity;
      componentInfo.getPackageName();

You will need the following permission on your manifest:
uses-permission android:name=”android.permission.GET_TASKS”

14 thoughts on “Getting info about your currently running activities

  1. I have an application, where I need to change the language through a
    settings menu. The problem is that the current activity don’t show
    data in new language only if I go back (by back button)to the main
    activity. I ask if there is any solution to reload the current
    activity(and other)…I want anywhere in my app change the language(by
    using a menu)and have data in new language without going back to main
    activity.(I must still in the current activity witch show data and
    resouce in new language)

    This is the code that I write it in every activity:

    new DialogInterface.OnClickListener() {

    public void onClick(DialogInterface dialog,int which) {

    switch(Item_id){

    case 0:
    Toast.makeText(getApplicationContext(),
    Item_name,Toast.LENGTH_SHORT).show();

    // Change locale settings on the device
    locale = new Locale(“En”);
    Locale.setDefault(locale);
    config = new Configuration();
    config.locale = locale;

    getBaseContext().getResources().updateConfiguration(config,
    getBaseContext().getResources().getDisplayMetrics());

    case 1:
    ………………………………………..
    }
    }

  2. You can update the the shared preferences and have your activities implement the SharedPreferenceChangeListener.
    SharedPreferences.OnSharedPreferenceChangeListener

    When that trigers you should just be able to update your views in the current activity.

    Hope that helps

  3. hi

    how to find out which activity is currently running and can you explain the concept

    gowtham

  4. Hi Gowtham,
    It works like this:
    The activityManager provides a list of tasks that are currently running on your phone.
    A task can be thought of as a collection of related activities. Most commonly your app will have it’s own task.

    You get a reference to the activity manager like this:

    ActivityManager am = (ActivityManager) this
    .getSystemService(ACTIVITY_SERVICE);

    One you have that, you can ask the activity manager to give you a list of running tasks.
    In this case I pass a one because I’m only interested in the first running task.
    (The currently running task will always show up first)

    The activity manager gives you this info in a list of activitymanager.runningtaskinfo objects….

    // get the info from the currently running task
    List taskInfo = am.getRunningTasks(1);

    The runningtaskinfo object has some additional methods to help you identify your activity. of interest is a reference to the topActivity which provides info about the activity. You can think of topActivity as the most likely activity to have the intent filter of Main or Launcher.

    ComponentName componentInfo = taskInfo.get(0).topActivity;

    I hope that helps.

  5. ActivityManager am = (ActivityManager)context.getSystemService(Activity.ACTIVITY_SERVICE);
    List taskInfo = am.getRunningTasks(5); //1
    for (RunningTaskInfo info : taskInfo) {
    Toast.makeText(context, “\nURRENT Activity:” + info.topActivity.getClassName(), Toast.LENGTH_LONG).show();
    }

  6. use the above code to get the topactivity name when you working inside a BroadcastReceiver
    this is very useful when you develop a SMS service and onReceive() if you wanna find the top activity.

  7. Is there any way to get an instance of the currently running activity , so that i could call methods like dispatch events ?

  8. Thanx Nelson…100% correct.. jUst mention that we will also need uses-permission android:name=”android.permission.GET_TASKS”

  9. Hi ahmed,
    Hmm i suppose it COULD be possible. I guess from within a service the trick would be to get access to the context.
    I’m pretty curious about this now. I’m not sure how but I think there could be some tricky way of doing this.

  10. hi, is it possible to get farther values with ActivityManager?
    for example, the activity uses a farther class, which is not an activity, but gets a valuable String(name of the item, or something),
    Can I get that String(the name of the current item displayd in a view) also?

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>