My android app has 2 launcher icons.
I am clicking the launcher icon 1 which open the first activity.
When i take the app to background by hitting home button & click on launcher icon 2, I am seeing the App is being bought to foreground instead of opening the activity corresponding to launcher icon 2.
But I have requirement to open the corresponding activity even if the app is in background.
I tried setting android:clearTaskOnLaunch="true" & using onNewIntent, but this is not being trigger when app is resumed via launcher icon 2. Could some one help me on how to solve this?
---------------------------- PROCESS STARTED (10945) for package com.example.twolaunchericonapplication ----------------------------
//click on launcher icon 1
2023-10-17 07:51:09.581 10945-10945 First com...le.twolaunchericonapplication E onCreate
2023-10-17 07:51:09.602 10945-10945 First com...le.twolaunchericonapplication E onResume: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.twolaunchericonapplication/.MainActivity bnds=[582,785][859,1205] }
// click launcher icon 1 again
2023-10-17 07:51:22.113 10945-10945 First com...le.twolaunchericonapplication E onNewIntentIntent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10600000 cmp=com.example.twolaunchericonapplication/.MainActivity bnds=[582,785][859,1205] }
2023-10-17 07:51:22.113 10945-10945 First com...le.twolaunchericonapplication E onResume: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.twolaunchericonapplication/.MainActivity bnds=[582,785][859,1205] }
// click launcher icon 2
2023-10-17 07:51:31.277 10945-10945 First com...le.twolaunchericonapplication E onResume: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.example.twolaunchericonapplication/.MainActivity bnds=[582,785][859,1205] }
<activity
android:name=".MainActivity"
android:exported="true"
android:label="First Activity"
android:launchMode="singleTop"
android:clearTaskOnLaunch="true"
android:theme="@style/Theme.TwoLauncherIconApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:exported="true"
android:launchMode="singleTop"
android:clearTaskOnLaunch="true"
android:label="Second Activity"
android:theme="@style/Theme.TwoLauncherIconApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
class MainActivity : ComponentActivity() {
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Log.e("First","onNewIntent"+intent)
}
override fun onResume() {
super.onResume()
Log.e("First","onResume: "+intent)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.e("First","onCreate")
setContent {
TwoLauncherIconApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("First")
}
}
}
}
}
class SecondActivity : ComponentActivity() {
override fun onNewIntent(intent: Intent?) {
super.onNewIntent(intent)
Log.e("Second","onNewIntent"+intent)
}
override fun onResume() {
super.onResume()
Log.e("Second","onResume :"+intent)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.e("Second","onCreate")
setContent {
TwoLauncherIconApplicationTheme {
// A surface container using the 'background' color from the theme
Surface(
modifier = Modifier.fillMaxSize(),
color = MaterialTheme.colorScheme.background
) {
Greeting("Second")
}
}
}
}
}
@Composable
fun Greeting(name: String, modifier: Modifier = Modifier) {
Text(
text = "Hello $name!",
modifier = modifier
)
}
@Preview(showBackground = true)
@Composable
fun GreetingPreview() {
TwoLauncherIconApplicationTheme {
Greeting("Second")
}
use launchMode="singleTask"
<activity
android:name=".MainActivity"
android:exported="true"
android:label="First Activity"
android:launchMode="singleTask"
android:theme="@style/Theme.TwoLauncherIconApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:exported="true"
android:label="Second Activity"
android:launchMode="singleTask"
android:theme="@style/Theme.TwoLauncherIconApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
or use taskAffinity if you want to have several tasks.
<activity
android:name=".MainActivity"
android:exported="true"
android:label="First Activity"
android:taskAffinity="com.example.twolaunchericonapplication.MainActivity"
android:theme="@style/Theme.TwoLauncherIconApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:exported="true"
android:label="Second Activity"
android:taskAffinity="com.example.twolaunchericonapplication.SecondActivity"
android:theme="@style/Theme.TwoLauncherIconApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>