1

In android kotlin, I had changed the app icon and app name using activity alias concept. But after switch the icons and app name. When the app is minimized the app is not found in my background. Attached video link for issue reference:https://drive.google.com/file/d/1V59pBDjhC6kvK5uvnRH3Miqd291pbN2g/view?usp=sharing

My Mainactivity.kt and android.xml code below:

  packageManager.setComponentEnabledSetting(ComponentName("com.example.batpoc","com.example.batpoc.MainActivityOneLaunch"), PackageManager.COMPONENT_ENABLED_STATE_ENABLED,PackageManager.DONT_KILL_APP)
    packageManager.setComponentEnabledSetting(ComponentName("com.example.batpoc","com.example.batpoc.MainActivity"), PackageManager.COMPONENT_ENABLED_STATE_DISABLED,PackageManager.DONT_KILL_APP)




<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.batpoc">
<!-- io.flutter.app.FlutterApplication is an android.app.Application that
     calls FlutterMain.startInitialization(this); in its onCreate method.
     In most cases you can leave this as-is, but you if you want to provide
     additional functionality it is fine to subclass or reimplement
     FlutterApplication and put your custom class here. -->
<application
    android:name="io.flutter.app.FlutterApplication"
    android:label="batpoc"
    android:icon="@mipmap/ic_launcher">
    <activity
        android:name=".MainActivity"
        android:launchMode="singleTop"        
        android:theme="@style/LaunchTheme"
        android:configChanges="orientation|keyboardHidden|keyboard|screenSize|smallestScreenSize|locale|layoutDirection|fontScale|screenLayout|density|uiMode"
        android:hardwareAccelerated="true"
        android:windowSoftInputMode="adjustResize">
          <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity>
      <activity-alias
            android:name=".MainActivityOneLaunch"
            android:enabled="false"
            android:roundIcon="@mipmap/th"
            android:icon="@mipmap/th"
            android:label="One Launch"
            android:targetActivity=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
             <category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.LAUNCHER"/>
        </intent-filter>
    </activity-alias>

</application>

Please guide.Thanks in advance.

Tester12
  • 911
  • 1
  • 11
  • 23

1 Answers1

1

Create such config in Manifest.xml

<activity android:name="package.name.MainActivity"
 android:screenOrientation="portrait"
 android:label="@string/app_name"
 android:theme="@style/CustomTheme"
 android:launchMode="singleTask">
  <intent-filter>
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity>

<activity-alias android:label="@string/app_name_default" 
 android:icon="@drawable/icon_default" 
 android:name=".MainActivity-Default"
 android:enabled="true"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

<activity-alias android:label="@string/app_name_flavor_one" 
 android:icon="@drawable/icon_flavor_one" 
 android:name=".MainActivity-Flavor-One"
 android:enabled="false"
 android:targetActivity=".MainActivity">
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>   
</activity-alias>

Now you can switch between those two aliases, therefore we will change app icon or/and name. To switch from Default to Flavor-One use this code.

getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Flavor-One"), 
        PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
 getPackageManager().setComponentEnabledSetting(
    new ComponentName("package.name", "package.name.MainActivity-Default"), 
        PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);

Keep in mind that you have to track that only one alias will be enabled at a time

Muntasir Aonik
  • 1,800
  • 1
  • 9
  • 22