-1

Hi I am working on my custom launcher. My custom launcher is set to default launcher and I need to call the android's basic launcher once a user clicks a button programmatically. I have searched out a lot and found out I cannot just turn off the application so finish() and exit(0) are not working. All I found it I just need to call the android's basic launcher but it is very hard to get the final answers. Here is what I have:

    PackageManager pm=getPackageManager();
    Intent main=new Intent(Intent.ACTION_MAIN, null);
    List<ResolveInfo> launchables=pm.queryIntentActivities(main, 0);
    Collections.sort(launchables, new ResolveInfo.DisplayNameComparator(pm)); 
    for(int i=0;i<launchables.size();i++) {
        //find android's basic launcher package
        if(launchables.get(i).toString().contains("com.android.launcher")) {
            //open a package
            ResolveInfo launchable = launchables.get(i);
            Util.print(launchable.resolvePackageName);
            Intent intent = pm.getLaunchIntentForPackage(launchable.resolvePackageName);
            startActivity(intent);
        }
    }

I know this code is quite silly to find and call a package. This code calls the Android's setting and second call makes an error. I am pretty sure this code is not good, I just want you to know what I am trying to do now. Can anyone tell me how to open Android's basic launcher?

Edited with temporary solution

    ComponentName name=new ComponentName("com.android.launcher", "com.android.launcher2.Launcher");
    Intent intent = new Intent();
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
    intent.setComponent(name);
    startActivity(intent);  
DVN
  • 114
  • 1
  • 9
  • `I need to call the android's basic launcher once a user clicks a button programmatically` - why? What are you trying to do? For the error you have, no-one can help unless you tell us what the error is and include the stack trace form logcat in your question. – Simon Dec 18 '14 at 18:26
  • Dude. I said I am trying to call Android's basic launcher programmatically when my custom launcher is set to default. I just found a solution and updated. Fixing error was not the goal of mine. Thanks for your reply and help – DVN Dec 18 '14 at 19:23

1 Answers1

1

I am trying to call Android's basic launcher

There is no concept as "Android's basic launcher". There are thousands of Android device models. There are dozens, if not hundreds, of pre-installed home screen implementations. One is named com.android.launcher, and it will be on few devices.

Edited with solution

No, that is not a solution. As I noted above, few devices have com.android.launcher. And some that do have it there for historical reasons, and it does not reflect what the user would think of as their original home screen implementation.

You are welcome to use PackageManager and queryIntentActivities() to find what activities have a MAIN/HOME <intent-filter>. If there are two matches, one should be your activity, and the other would be the original home screen. However, it is entirely possible that there are three or more matches, in which case you should ask the user which home screen implementation to bring up.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Or maybe just invoke the intent implicitly so Android does the asking for you? – Machinarius Dec 18 '14 at 19:33
  • @Machinarius: At best, you'd need to use `createChooser()`, as I presume that the OP's home screen implementation is the user's default, so just a straight-up `startActivity()` call would route the user right back to where she started. The downside of the stock way of using `createChooser()` is that the OP's home screen would *also* appear in the list, as it's a `MAIN`/`HOME` activity, which might be confusing to the user. – CommonsWare Dec 18 '14 at 19:37