Search code examples
androidandroid-intentlauncherandroid-launcher

Load launcher app again?


I have a custom launcher. When I install it, I open it for the first time and use the following code to load the intent chooser for the launchers:

Intent localIntent = new Intent(Intent.ACTION_MAIN);
localIntent.addCategory(Intent.CATEGORY_HOME);
localIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(localIntent);

My app will show the all the launchers. Then I choose the other launcher, the one that's notm y launcher, and set it to default. Then the next time the code above runs, the intent chooser does not appear and I cannot change the default launcher. Here is what I've tried to fix it:

private void callIntenChoiceLauncher(Context c) 
{
    PackageManager p = getPackageManager();
    ComponentName cN = new ComponentName(c, RunDefaultLauncher.class);
    p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);

    Intent selector = new Intent(Intent.ACTION_MAIN);
    selector.addCategory(Intent.CATEGORY_HOME);        
    c.startActivity(selector);

    p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

It's not working.


Solution

  • In this code:

    c.startActivity(selector);
    p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
    

    when you call startActivity(), the activity isn't started immediately. What this does is just tell the Android framework that you would like to start another activity at the next possible time that the framework gets control. In the next line you disable the component so that when the Android framework goes to start the activity, your launcher component has been disabled again.

    The call to startActivity() is not synchronous.