我们的联想平板电脑无法固定 Home 活动所在的任务。解决方案是在两个不同的任务中进行两个活动。
家庭活动
此活动在启动时作为启动器启动,其唯一责任是立即打开主要活动。请注意,它是透明的并且具有不同的任务亲和性。
<activity
android:name=".HomeActivity"
android:clearTaskOnLaunch="true"
android:configChanges="orientation|screenSize"
android:launchMode="singleTask"
android:resumeWhilePausing="true"
android:stateNotNeeded="true"
android:taskAffinity="${applicationId}.home"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
onCreate
该活动也将在其中启动主活动onNewIntent
(因为它是一个singleTask
活动)。这是 Kotlin 中的代码:
class HomeActivity : Activity() {
override fun onNewIntent(intent: Intent) {
super.onNewIntent(intent)
handleIntent(intent)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
handleIntent(intent)
}
private fun handleIntent(intent: Intent) {
val i = packageManager.getLaunchIntentForPackage(packageName)
startActivity(i)
}
}
主要活动
这是您的主要活动 - 您的应用程序的主要入口点,它可以从任何启动器启动。它具有默认的任务关联性(等于应用程序 ID)。
<activity
android:name=".webview.activity.RealWebViewActivity"
android:clearTaskOnLaunch="true"
android:configChanges="orientation|screenSize"
android:exported="false"
android:launchMode="singleTask"
android:resumeWhilePausing="true"
android:stateNotNeeded="true">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>