Reputation: 1561
I need to get the name of launcher activity to launch the activity from my application. Any solution
Upvotes: 30
Views: 32245
Reputation: 7435
Use the following code to get the launcher activity of all packages:
final PackageManager pm = getPackageManager();
Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
List<ResolveInfo> appList = pm.queryIntentActivities(mainIntent, 0);
Collections.sort(appList, new ResolveInfo.DisplayNameComparator(pm));
for (ResolveInfo temp : appList) {
Log.v("my logs", "package and activity name = "
+ temp.activityInfo.packageName + " "
+ temp.activityInfo.name);
}
Upvotes: 27
Reputation: 351
This is the easiest solution you can use. And it works perfectly.`
private String getLauncherActivityName(){
String activityName = "";
final PackageManager pm = getPackageManager();
Intent intent = pm.getLaunchIntentForPackage(getPackageName());
List<ResolveInfo> activityList = pm.queryIntentActivities(intent,0);
if(activityList != null){
activityName = activityList.get(0).activityInfo.name;
}
return activityName;
}
Upvotes: 3
Reputation: 46943
Even though the answers above answer directly the OP's question I would like to add my two cents:
/** Backwards compatible method that will clear all activities in the stack. */
public void startLauncherActivity(Context context) {
PackageManager packageManager = context.getPackageManager();
Intent intent = packageManager.getLaunchIntentForPackage(context.getPackageName());
ComponentName componentName = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(componentName);
context.startActivity(mainIntent);
}
Here I do not only get the launcher activity of the application, but also am clearing all the backstack of the activities (which is what I actually needed when I triggered the launcher activity). I call this in case of expired auth token for example.
Important thing is to use IntentCompat
, otherwise one has to resort to Intent
flag Intent.FLAG_ACTIVITY_CLEAR_TASK
, which is introduced only in API 11.
Upvotes: 19
Reputation: 1615
late but the better way it will give the exact intent to launch an activity
PackageManager pm = getPackageManager();
Intent intent=pm.getLaunchIntentForPackage(pacakgeName);
startActivity(intent);
Upvotes: 63