14

In my app I want to change the icon of the app based on the notification received. i.e if there are 2 notifications received, then the app icon having "2" in it will be shown in home screen of the device.

For this I have used activity alias. I have added following intent filters for the activity alias:

    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>

And to make a activity-alias active I have used following code:

This is to enable the activity alias:

 ComponentName componentName = new ComponentName("PACKAGE_NAME_HERE",
                    lastEnabled);
            pm.setComponentEnabledSetting(componentName,
                    PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                    PackageManager.DONT_KILL_APP);
            Utility.setLastEnabled(lastEnabled,context); 

This will disable previously enabled activity alias

componentName = new ComponentName(
            "PACKAGE_NAME_HERE", prevLastEnabled);
    pm.setComponentEnabledSetting(componentName,
            PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
            PackageManager.DONT_KILL_APP);

But as soon the app icon changes, my app closes without any exception. I have used dont kill app here, still it closes the app. Can anyone please suggest me if I am doing anything wrong.Thanks.

2 Answers 2

4

Your Activity is being closed probably because you have enabled/disabled your target Activity. Note that the App (Application Process) has not been killed. Anyways this is what I did to stop the Activity from closing:

In general you want to do the following:

  1. Use just activity-aliases for icons
  2. Remove the category "launcher" from the target Activity
  3. Only enable/disable activity-aliases

Your AndroidManifest.xml should look something like this:

<activity android:name=".MainActivity" ... >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
  </intent-filter>
</activity>

<activity-alias android:name=".No-Icon-Badge"
                android:targetActivity=".MainActivity"
                android:enabled="true" 
                ... >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity-alias>

<activity-alias android:name=".Icon-Badge-1"
                android:targetActivity=".MainActivity"
                android:enabled="false" 
                ... >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity-alias>

<activity-alias android:name=".Icon-Badge-2"
                android:targetActivity=".MainActivity"
                android:enabled="false"
                ... >
  <intent-filter>
    <action android:name="android.intent.action.MAIN" />
    <category android:name="android.intent.category.LAUNCHER" />
  </intent-filter>
</activity-alias>

...

So your MainActivity is enabled (keep it that way), but has no LAUNCHER category so it won't show up. Also the first activity-alias is enabled (this is your default) and all the other ones are disabled.

Good luck!

4
  • 2
    Your suggestion didn't work for my case, but gave me an idea - I won't call disable activity-aliases when my activity is showing, but after it's been finish()-ed. This fixed the issue for me Mar 20, 2020 at 17:50
  • 1
    @user3118604 i am trying to do something similar here. Though as for you my app is just closing after enable/disable the component. My launcher activity (SplashActivity) is already finished though when i try to change the icon and if i don't disable it then i end up having 2 different app icons. Any tips that might be helpful for me?
    – makis.k
    Oct 29, 2020 at 11:43
  • I could not make the alias thing work reliably. Ended up using "router" pattern - single LAUNCHER activity from which I'm launching one of the two other activities (according to the business logic) Oct 30, 2020 at 12:45
  • App gets killed by OS on Android 10+
    – Dinesh.P
    Dec 31, 2021 at 7:22
0

You can use the FLAG_ACTIVITY_NEW_TASK for the intent you want to show and finish() the activity from which you startActivity(intent)

finish()
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.