备注 :
MainActivity 正常带界面的UI
MainActivityBt 和 MainActivityUsb 是透明的,即 android:theme="@style/TranslucentTheme"
###场景1:只有MainActivity 设置成:android:launchMode="singleTask"
点击顺序:MainActivity > home键 > MainActivityBt > home键 > MainActivityUsb
日志如下:
2024-03-03 21:22:11.724 15131-15131 MultipleIcon pid-15131 D MainActivity.onCreate
2024-03-03 21:22:11.729 15131-15131 MultipleIcon pid-15131 D MainActivity.onStart
2024-03-03 21:22:11.730 15131-15131 MultipleIcon pid-15131 D MainActivity.onResume
---------------------------- PROCESS STARTED (15131) for package com.ifeng.multipleicon ----------------------------
2024-03-03 21:22:14.817 15131-15131 MultipleIcon com.ifeng.multipleicon D MainActivity.onStart
2024-03-03 21:22:14.817 15131-15131 MultipleIcon com.ifeng.multipleicon D MainActivity.onResume
2024-03-03 21:22:17.948 15131-15131 MultipleIcon com.ifeng.multipleicon D MainActivity.onStart
2024-03-03 21:22:17.949 15131-15131 MultipleIcon com.ifeng.multipleicon D MainActivity.onResume
日志简化图: 并没有执行MainActivityBt.onCreate 和 MainActivityUsb.onCreate;而且MainActivity只会执行一次。
###场景2:三个都设置成:android:launchMode="singleTask"
点击顺序:MainActivity > home键 > MainActivityBt > home键 > MainActivityUsb
2024-03-03 21:26:49.050 15690-15690 MultipleIcon pid-15690 D MainActivity.onCreate
2024-03-03 21:26:49.057 15690-15690 MultipleIcon pid-15690 D MainActivity.onStart
2024-03-03 21:26:49.057 15690-15690 MultipleIcon pid-15690 D MainActivity.onResume
---------------------------- PROCESS STARTED (15690) for package com.ifeng.multipleicon ----------------------------
2024-03-03 21:26:52.114 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivityBt.onCreate
2024-03-03 21:26:52.117 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivity.onStart
2024-03-03 21:26:52.136 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivity.onNewIntent
2024-03-03 21:26:52.136 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivity.onResume
2024-03-03 21:26:56.130 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivityUsb.onCreate
2024-03-03 21:26:56.133 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivity.onStart
2024-03-03 21:26:56.143 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivity.onNewIntent
2024-03-03 21:26:56.144 15690-15690 MultipleIcon com.ifeng.multipleicon D MainActivity.onResume
日志简化图: 都有执行MainActivityBt.onCreate 和 MainActivityUsb.onCreate;而且MainActivity也成功跳转了!
!!!注意点:
#1. 多图标入口,需要跳转对应页面,入口Activity需要设置成singleTask, 否则只会唤起现有后台的页面。
#2. 多图标入口,如果是公用一个页面,刷新数据需要在onCreate 和 onNewIntent 中刷新UI。
详细代码:
Manifest配置文件:
<application
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MultipleIconApplication"
tools:targetApi="31">
<activity
android:name=".MainActivityBt"
android:label="BT"
android:exported="true"
android:launchMode="singleTask"
android:theme="@style/TranslucentTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivityUsb"
android:label="USB"
android:exported="true"
android:launchMode="singleTask"
android:theme="@style/TranslucentTheme">
<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:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/Theme.MultipleIconApplication">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
MainActivity相关刷新UI代码 :
class MainActivity : AppCompatActivity() {
private val type : String?
get() = intent.getStringExtra("TYPE")
// 第一次打开需要在这里刷新
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
Log.d("MultipleIcon", "MainActivity.onCreate")
syncTypeDisplay()
}
override fun onStart() {
super.onStart()
Log.d("MultipleIcon", "MainActivity.onStart")
}
// 当非首次进入时,需要在这里刷新
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
// 需要使用setIntent(intent)赋值给当前Activity的Intent.否则,后续的getIntent()都是得到老的Intent。
setIntent(intent)
syncTypeDisplay()
Log.d("MultipleIcon", "MainActivity.onNewIntent")
}
private fun syncTypeDisplay() {
findViewById<TextView>(R.id.tv).text = when(type){
"BT" -> {
"蓝牙音乐"
}
"USB" ->{
"USB音乐"
}
else -> {
"收音机"
}
}
}
override fun onResume() {
super.onResume()
Log.d("MultipleIcon", "MainActivity.onResume")
}
}