20Jan/1111
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"

April 4th, 2011 - 02:46
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:
………………………………………..
}
}
April 5th, 2011 - 20:24
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
June 20th, 2011 - 06:48
hi
how to find out which activity is currently running and can you explain the concept
gowtham
July 7th, 2011 - 14:43
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 taskInfo = am.getRunningTasks(1);
List
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.
December 21st, 2011 - 04:08
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();
}
December 21st, 2011 - 04:11
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.
December 21st, 2011 - 09:32
@Suren thanks! I’m glad you found it helpful. I appreciate you comment :)
April 26th, 2012 - 11:37
Is there any way to get an instance of the currently running activity , so that i could call methods like dispatch events ?
April 28th, 2012 - 05:47
Thanx Nelson…100% correct.. jUst mention that we will also need uses-permission android:name=”android.permission.GET_TASKS”
April 28th, 2012 - 10:27
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.
April 28th, 2012 - 19:35
Thanks shahzadrocks! I’ve updated the post with your suggestion