Android - show launcher chooser dialog on demand

Viewed 71

I would like to show the launcher chooser dialog only on a custom button click and disable it by default. To disable it by default I added an activity alias disabled component inside my app manifest. The alias component has the category 'DEFAULT' inside intent filter declaration.

AndroidManifest.xml

        <intent-filter>
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity>

    <activity-alias
        android:enabled="false"
        android:name=".DefaultActivity"
        android:targetActivity=".MainActivity">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.categrory.DEFAULT"/>
        </intent-filter>

    </activity-alias>

On custom button click the disabled component is enabled:

fun resetDefault() {

    val packageManager: PackageManager? = fragment.activity?.packageManager

    val compName = fragment.activity?.packageName?.let {
        ComponentName(
            it,
            "$it.DefaultActivity"
        )
    }
    if (compName != null) {
        packageManager?.setComponentEnabledSetting(
            compName,
            PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
            PackageManager.DONT_KILL_APP
        )
    }

    val selector = Intent(Intent.ACTION_MAIN)
    selector.addCategory(Intent.CATEGORY_HOME)

    selector.flags = Intent.FLAG_ACTIVITY_NEW_TASK

    fragment.activity?.startActivity(selector)

    compName?.let {
        packageManager?.setComponentEnabledSetting(
            it,
            PackageManager.COMPONENT_ENABLED_STATE_DEFAULT,
            PackageManager.DONT_KILL_APP
        )
    }
    
}

The problem is that the launcher chooser dialog is not showing up and the application is closed insteed. Could anyone tell me what is missing from the current implementation?

0 Answers
Related