接前一篇,已经知道:
Runnable startActivity = new Runnable() {
@Override
public void run() {
Intent intent = new Intent();
intent.setClassName("com.example.root.belaunched",
"com.example.root.belaunched.ThirdActivity");
startActivity(intent);
intent.setClassName("com.example.root.belaunched",
"com.example.root.belaunched.SecondActivity");
startActivity(intent);
FirstActivity.this.finish();
}
};
startActivity的先后顺序决定了哪个task在前台。在项目中SecondActivity是主界面,它不能被挡在DoLaunch界面的下面,那样行为古怪,客户体验糟糕。SecondActivity最后启动当然好,然而设计要求必须是SecondActivity先启动,ThirdActivity后启动。最后的解决办法是:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.root.belaunched">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".FirstActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.belaunched.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SecondActivity"
android:launchMode="singleTask">
</activity>
<activity
android:name=".ThirdActivity">
</activity>
</application>
</manifest>
即把应用的入口Activity设成singleTask,因为它的taskAffinity跟DoLaunch不同,所以会创建一个新的task,然后FirstActivity、SecondActivity、ThirdActivity都在这个task中,SecondActivity自然最后必定在DoLaunch之上,不会出现back键弹出DoLaunch才能进SecondActivity的现象。
如此一来Activity顺序正常了:FirstActivity ——finish —— ThirdActivity —— back键 —— SecondActivity ——back键—— DoLaunch