Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove Activity as Default Launcher

I set my activity as a default launcher to intercept home button clicks like so:

<activity
    android:name=".ExampleActivity"
    android:label="@string/app_name">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
        <category android:name="android.intent.category.HOME" />        
        <category android:name="android.intent.category.DEFAULT" />               
    </intent-filter>
</activity>

When my activity, ExampleActivity is launched, if i click the home key, I get prompted to choose. If I select make this my default and chose my activity, I am stuck In my activity as desired.

The problem is, when I leave the activity, I try to remove my activity from the default launcher, but am unsuccessful.

I have tried:

ComponentName componentName = new ComponentName( 
                    "com.example.exampleactivity", 
                    "com.example.exampleactivity.class");

pm.setComponentEnabledSetting(componentName, PackageManager.COMPONENT_ENABLED_STATE_DEFAULT, PackageManager.DONT_KILL_APP);

And:

PackageManager pm = getActivity().getPackageManager();
             ComponentName name = new ComponentName(this, "com.example.exampleactivity.class");
             pm.setComponentEnabledSetting(name, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, 0);

But my designation for the home is never removed.

Does anyone have a working way to fix the above?

I only wan't the home button to be default for a specific activity, not my entire application. When I leave the activity, it should be removed and restored to default.

like image 489
Mike Mackintosh Avatar asked Sep 26 '12 03:09

Mike Mackintosh


People also ask

How do I disable the default launcher?

Go to Settings > Apps/Applications > scrolll down to the launcher that is the default for your Android device > scroll down and tap on 'Clear defaults'. Defaults are set when you are asked to set a launcher just once or always.

How do I get back to normal launcher?

Resetting your Android phone back to the default launcher is as easy as 1,2,3. In fact, you just need to uninstall the launcher, and it' all. After uninstalling the launcher, your default launcher will automatically be set for your device.


2 Answers

if it's your app that you're clearing its defaults , you can simply call :

getPackageManager().clearPackagePreferredActivities(getPackageName());

then , in order to show the dialog of choosing which launcher to use , use:

final Intent intent=new Intent();
intent.setAction(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);
like image 144
android developer Avatar answered Oct 05 '22 07:10

android developer


This solution is a clever way of doing it: Clearing and setting the default home application

The code in onResume() basically goes like this:

    ComponentName componentName = new ComponentName(MyActivity.this, FakeHome.class);
    if (!isMyLauncherDefault()) {
        Log.e(TAG, "MyActivity is not default home activity!");

        // toggle fake activity
        PackageManager pm = getPackageManager();
        int flag = ((pm.getComponentEnabledSetting(componentName) == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) ? PackageManager.COMPONENT_ENABLED_STATE_DISABLED
                : PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
        pm.setComponentEnabledSetting(componentName, flag, PackageManager.DONT_KILL_APP);

        // start home activity to enable chooser
        Intent selector = new Intent(Intent.ACTION_MAIN);
        selector.addCategory(Intent.CATEGORY_HOME);
        startActivity(selector);
    }

and the method isMyLauncherDefault() is taken from here: How to check if my application is the default launcher

like image 33
ToBe_HH Avatar answered Oct 05 '22 05:10

ToBe_HH