16

I'm looking for a way to launch the default android launcher programatically, something perhaps like the code below. Or do I have to add something to the manifest file? Thanks!

Intent intent = new Intent();
intent.setClassName("com.android.launcher", "Launcher");
startActivity(intent);
dosa
  • 635
  • 2
  • 6
  • 11

5 Answers5

23

Have you tried this?

startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_LAUNCHER));

(I haven't tried it myself, because my use case is a little more complicated---I've replaced the launcher, and I want to call the old launcher...)

I've also discovered that you can use the package manager to look through all activities that meet some intent filter criteria. For example, if you want to find all the activities marked as the home default home activity, use this:

Intent intent=null;
final PackageManager packageManager=getPackageManager();
for(final ResolveInfo resolveInfo:packageManager.queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY))
{
    if(!getPackageName().equals(resolveInfo.activityInfo.packageName))  //if this activity is not in our activity (in other words, it's another default home screen)
    {
        intent=packageManager.getLaunchIntentForPackage(resolveInfo.activityInfo.packageName));
        break;
    }
}

Note that I have replaced the default home screen on my device---that's why I have to make sure the activity I found is not the activity that's running! If you haven't replaced the default home activity, you don't need this check---just use the first (and probably the only) default home activity.

(Note that I still can't launch the old launcher from my launcher, perhaps because the old launcher keeps a record of the default launcher, which is my new launcher, and simply calls back to it. I don't know. But at least it doesn't crash, and I would guess that, if you haven't replaced the old home screen, it just might work.)

Garret Wilson
  • 18,219
  • 30
  • 144
  • 272
  • 5
    Please note that the getLaunchIntentForPackage method will not return the launcher intent since it only checks for INFO and LAUNCH categories while the Laucher has the HOME category in intent. This means that the call in the if inside the for will return a null intent. Replacing the intent initialization with the code below did the trick for me. intent = new Intent().addCategory(Intent.CATEGORY_HOME).setAction(Intent.ACTION_MAIN) .setClassName(resolveInfo.activityInfo.packageName, resolveInfo.activityInfo.name); – r1k0 Aug 30 '13 at 07:13
  • 1
    hey @GarretWilson, did you figure out how to launch the non-default launcher? – isaganiesteron Jun 10 '14 at 04:50
  • 1
    @isaganiesteron, sorry, I haven't touched this code in years. I don't have any updates at the moment. – Garret Wilson Jun 10 '14 at 17:14
  • 1
    To start the stock launcher, add this: ` intent.setPackage("com.android.launcher");` – Greg Ennis Jan 12 '15 at 16:16
4

Following Garret Wilson's answer, here's an ugly one-liner, assuming context is your application context:

context.startActivity(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME).setPackage(context.getPackageManager().queryIntentActivities(new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY).get(0).activityInfo.packageName));

This code assumes that the original system home activity is always the first result returned by queryIntentActivities, whereas the accepted answer returns the first home activity not belonging to the running package.

It's still unclear how to cleanly get the system home activity. Some threads mention that getPackageManager().resolveActivity(intent, flags) can be used for this, but it seems PackageManager.MATCH_SYSTEM_ONLY cannot be used with this method.

zeiky
  • 101
  • 3
  • 1
    The system home activity can be distinguished from the others by comparing it´s signature with any other known system app: both will have the same SHA1 hash. – AlxDroidDev Jun 15 '18 at 18:15
1
=> In kotlin add below code in onDestroy method of appCompactActvity use to make your app as default launcher, 

override fun onDestroy() {
        var intent = Intent(Intent.ACTION_MAIN)
        var packageManager: PackageManager = packageManager
        for (resolveInfo in packageManager.queryIntentActivities(Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME), PackageManager.MATCH_DEFAULT_ONLY)) {
            if (packageName != resolveInfo.activityInfo.packageName)  //if this activity is not in our activity (in other words, it's another default home screen)
            {
                startActivity(intent)
            }
            break
        }
        super.onDestroy()
    }
Shraddha Patel
  • 149
  • 1
  • 5
1
 startActivity( Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_HOME));
Suraj Rao
  • 29,388
  • 11
  • 94
  • 103
0

This code can open the app launcher,

   
Intent intentf = new Intent(Intent.ACTION_ALL_APPS);
intentf.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentf);
apollosoftware.org
  • 12,161
  • 4
  • 48
  • 69
Will Shaw
  • 22
  • 3