应用后台时双Launcher图标行为异常问题求助
问题:双Launcher图标点击后台应用未打开对应Activity
我的Android应用有2个Launcher图标,操作步骤如下:
- 点击Launcher图标1,打开MainActivity;
- 按下Home键将应用置于后台后,点击Launcher图标2,应用仅回到前台,并未打开对应的SecondActivity。
需求:即使应用处于后台,点击对应Launcher图标也需打开相应的Activity。
已尝试设置android:clearTaskOnLaunch="true"并使用onNewIntent方法,但点击Launcher图标2恢复应用时该方法并未触发。
日志信息
---------------------------- 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] }
Manifest配置
<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 { 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 { 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") } }
解决方案
问题根源在于:两个Launcher Activity默认共享同一个taskAffinity,系统会复用已有的Task将应用带到前台,而非启动目标Activity。需通过以下配置修改:
1. 修改Manifest配置
给两个Activity设置不同的taskAffinity,并将launchMode改为singleTask,移除clearTaskOnLaunch属性:
<activity android:name=".MainActivity" android:exported="true" android:label="First Activity" android:launchMode="singleTask" android:taskAffinity="com.example.twolaunchericonapplication.main" 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:taskAffinity="com.example.twolaunchericonapplication.second" android:theme="@style/Theme.TwoLauncherIconApplication"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
配置说明
taskAffinity:指定Activity所属的Task分组,不同值会让系统为每个Launcher图标创建独立的Task,点击时系统会匹配对应Task,而非复用已有Task。launchMode="singleTask":确保每个Activity在对应Task中仅存在一个实例,点击Launcher图标时,若对应Task已存在则将其带到前台并调用onNewIntent;若不存在则创建新Task和实例。
2. 验证效果
修改后:
- 点击图标1启动MainActivity,后台后点击图标2,系统会启动SecondActivity所在的独立Task;
- 再次点击对应图标,会直接唤起对应Task的Activity,并触发
onNewIntent方法。
内容的提问来源于stack exchange,提问作者RAMU
相关产品推荐
相关产品推荐

