Question

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.

Was it helpful?

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.

OTHER TIPS

Assuming you want to disable your home activity after the chooser has been shown so that the default launcher will react to the home button until you enable your activity:

Do not disable your home activity after startActivity. This should be done in the onDestroy() method of your home activity.

Modify onCreate() of your home activity:

public void onCreate(Bundle savedInstanceState) {
    /*check some flag in sharedPreferences or elsewhere 
    to determine if the activity was launched as part of the chooser process
    */
    finish();
}

Add onDestroy() to your home activity:

public void onDestroy() {
    //Disable the activity again
    PackageManager p = getPackageManager();
    ComponentName cN = new ComponentName(c, RunDefaultLauncher.class);
    p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
}

If you do not want to disable your home activity after the chooser was shown, simply do not call p.setComponentEnabledSetting(cN, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top