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
<activity-alias>
entries in their current manifest. I'm not yet sure how to explain the activation sequence, though. Indeed, after the first activation and restart, it never asks to do that again, which makes one wonder why it's not just set up like that from the start. – WoodiesetComponentEnabledSetting()
withCOMPONENT_ENABLED_STATE_DISABLED
versusCOMPONENT_ENABLED_STATE_DEFAULT
, as they only useCOMPONENT_ENABLED_STATE_DISABLED
with the default<activity-alias>
,HomeActivityDefault
; i.e., the only one that doesn't haveandroid:enabled="false"
set. The rest of them are set toCOMPONENT_ENABLED_STATE_DEFAULT
. Also, one of those other<activity-alias>
entries –HomeActivityTodoist
– is the same as the default, so maybe after... – WoodieDISABLED
setting on the default alias, the rest of the changes are justENABLED
orDEFAULT
between the rest of the<activity-alias>
es, which might not force a restart. That's all purely conjecture at this point, as I can't test anything atm. – Woodie