Tobi B.
Tobi B.

Reputation: 75

Change Android Launcher Icon like Instagram/Todoist

At Instagrams anniversary the users were possible to change the app icon. Even though this feature isn't available anymore Todoist has a similar working implementation of this feature.

I researched how to achieve this and got the second answer running: change application icon.

But this solution seems to come with drawbacks which I don't want. Some of these are listed here under the notes: blog change app icon. Furthermore, by using this solution my App gets killed every time I choose another icon with the activity-alias and I wasn't able to keep the app alive.

The behaviour of the Todoist App: First you have to activate the "change app icon dynamically"-feature which closes the app. Then after starting the app again the feature is available and you can change the app icon as you want.

As far as I know Instagrams implementation didn't even need this little restart, but I would be okay with that because this solution would be better than the one existing right now.

So how does the Todoist or Instagram approach work and how would an implementation look like?

Upvotes: 5

Views: 3391

Answers (1)

xo-tymoshenko
xo-tymoshenko

Reputation: 1

Thanks to Mike M.

I found a workaround to change the icon without closing the app. All you need is to create another config activity and set main activity's launch mode to single instance. (Main activity and its alias should be disabled)

<activity
        android:name=".config.ConfigActivity"
        android:exported="true"
        android:enabled="true"
        android:icon="@mipmap/ic_light"
        android:screenOrientation="sensorPortrait"
        tools:ignore="DiscouragedApi,LockedOrientationActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity
        android:name=".MainActivity"
        android:exported="true"
        android:enabled="false"
        android:icon="@mipmap/ic_light_round"
        android:launchMode="singleInstance"
        android:screenOrientation="sensorPortrait"
        tools:ignore="DiscouragedApi,LockedOrientationActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity-alias
        android:name=".MainActivityDark"
        android:exported="true"
        android:enabled="false"
        android:icon="@mipmap/ic_dark_round"
        android:targetActivity=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

After this you have to do some manipulations:

· Enable main activity in onCreate() and start it with some extra - Boolean:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    this.enableMainActivity()
    startActivity(
        Intent(this, MainActivity::class.java).apply {
            putExtra("is_config", true)
        }
    )
}

· Disable config activity in onCreate():

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    val isConfigNeeded = intent.getBooleanExtra("is_config", false)
    if (isConfigNeeded) {
        disableConfigActivity()
    }

    // UI
}

The only one step is ahead:

· Start main activity in config activity's onDestroy():

override fun onDestroy() {
    super.onDestroy()
    startActivity(Intent(this, MainActivity::class.java))
}

You can change the icon now by enabling/defaulting.

Thank you for the attention! Github repo: xo-tymoshenko/iconchange

Upvotes: 0

Related Questions