FLAG_ACTIVITY_NEW_TASK Label
Configuration form:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.open.android.task5">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="SecondActivity" />
</application>
</manifest>
Explain:
In this example, there are two MainActivities and EcondActivities.
Use cases:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Log.i("maweiqi", "onCreate: " + getClass().getSimpleName() +
" TaskId: " + getTaskId() + " hasCode:" + this.hashCode());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i("maweiqi", "onNewIntent: " + getClass().getSimpleName() +
" TaskId: " + getTaskId() + " hasCode:" + this.hashCode());
}
}
Note that the jump Intent adds the FLAG_ACTIVITY_NEW_TASK flag.
public class SecondActivity extends AppCompatActivity {
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(SecondActivity.this, MainActivity.class);
startActivity(intent);
}
});
Log.i("maweiqi", "onCreate: " + getClass().getSimpleName() +
" TaskId: " + getTaskId() + " hasCode:" + this.hashCode());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i("maweiqi", "onNewIntent: " + getClass().getSimpleName() +
" TaskId: " + getTaskId() + " hasCode:" + this.hashCode());
}
}
Test method:
MainActivity–> SecondActivity –> MainActivity–> SecondActivity
The output log is as follows:
onCreate: MainActivity TaskId: 73 hasCode:195694529
onCreate: SecondActivity TaskId: 73 hasCode:54326677
onCreate: MainActivity TaskId: 73 hasCode:121740648
onCreate: SecondActivity TaskId: 73 hasCode:5761837
Execute the following command adb shell dumpsys activity from the command line, with the following output:
Running activities (most recent first):
TaskRecord{e3d17e2 #73 A=com.open.android.task5 U=0 sz=4}
Run #3: ActivityRecord{a34fa00 u0 com.open.android.task5/.SecondActivity t73}
Run #2: ActivityRecord{21172da u0 com.open.android.task5/.MainActivity t73}
Run #1: ActivityRecord{b1e1e64 u0 com.open.android.task5/.SecondActivity t73}
Run #0: ActivityRecord{f015a6e u0 com.open.android.task5/.MainActivity t73}
mResumedActivity: ActivityRecord{a34fa00 u0 com.open.android.task5/.SecondActivity t73}
Test results:
1) All in the same task
2) Create all the different instances
So what if MainActivity jumps to remove FLAG_ACTIVITY_NEW_TASK? Add singleTask to the StockActivity manifest file, and the code and process are the same as before, only the manifest file configuration is posted.
The configuration is as follows:
<activity android:name="SecondActivity"
android:launchMode="singleTask"/>
Test method:
MainActivity–> SecondActivity –> MainActivity–> SecondActivity
The output log is as follows:
onCreate: MainActivity TaskId: 74 hasCode:195694529
onCreate: SecondActivity TaskId: 74 hasCode:54326677
onCreate: MainActivity TaskId: 74 hasCode:38155814
onNewIntent: SecondActivity TaskId: 74 hasCode:54326677
Execute the following command adb shell dumpsys activity from the command line, with the following output:
Running activities (most recent first):
TaskRecord{46d0de2 #74 A=com.open.android.task5 U=0 sz=2}
Run #1: ActivityRecord{b31602f u0 com.open.android.task5/.SecondActivity t74}
Run #0: ActivityRecord{f88d9dc u0 com.open.android.task5/.MainActivity t74}
mResumedActivity: ActivityRecord{b31602f u0 com.open.android.task5/.SecondActivity t74}
Test results:
1) The effects of FLAG_ACTIVITY_NEW_TASK and singleTask have different effects.
What if the android: task Affinity of the two activities are different? What effect will this lead to? Next, let's take an example to see the effect.
The configuration is as follows:
<activity android:name="SecondActivity"
android:taskAffinity="com.maweiqi.second"/>
The code goes back to the original MainActivity or adds Flag tags. Other omissions are as follows:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button).
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
Log.i("maweiqi", "onCreate: " + getClass().getSimpleName() +
" TaskId: " + getTaskId() + " hasCode:" + this.hashCode());
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Log.i("maweiqi", "onNewIntent: " + getClass().getSimpleName() +
" TaskId: " + getTaskId() + " hasCode:" + this.hashCode());
}
}
Test method:
MainActivity–> SecondActivity –> MainActivity–> SecondActivity
The output log is as follows:
onCreate: MainActivity TaskId: 77 hasCode:195694529
onCreate: SecondActivity TaskId: 78 hasCode:54326677
onCreate: MainActivity TaskId: 78 hasCode:38155814
Execute the following command adb shell dumpsys activity from the command line, with the following output:
Running activities (most recent first):
TaskRecord{65dda1d #78 A=com.maweiqi.second U=0 sz=2}
Run #2: ActivityRecord{845c9ed u0 com.open.android.task5/.MainActivity t78}
Run #1: ActivityRecord{f813328 u0 com.open.android.task5/.SecondActivity t78}
TaskRecord{dd8e492 #77 A=com.open.android.task5 U=0 sz=1}
Run #0: ActivityRecord{7a85f99 u0 com.open.android.task5/.MainActivity t77}
mResumedActivity: ActivityRecord{845c9ed u0 com.open.android.task5/.MainActivity t78}
Test results:
1) When we enter MainActivity for the second time and attempt to enter Second Activity from MainActivity, it has no effect and still remains in MainActivity. That is, the second MainActivity --> Second Activity never happened at all!
Result analysis:
1) When the android: task Affinity of the two activities that jump over each other is different, add FLAG_ACTIVITY_NEW_TASK: When the second activity is first started, a new task is created and the second activity is added to the task. This is the same effect as singleTask! But nothing happened when an attempt was made to re-enter Second Activity from MainActivity!
2) Why? Because the Second Activity instance already exists at this time, but the top of the task stack is MainActivity; and adding FLAG_ACTIVITY_NEW_TASK alone will not "delete the activity instance above the Second Activity" in the task, so there is no jump!
Let's add FLAG_ACTIVITY_CLEAR_TOP and see the effect again.
Modify the click events in MainActivity as follows:
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="SecondActivity"/>
Test method:
MainActivity–> SecondActivity –> MainActivity–> SecondActivity
Log Output
onCreate: MainActivity TaskId: 80 hasCode:58656936
onCreate: SecondActivity TaskId: 80 hasCode:212434303
onCreate: MainActivity TaskId: 80 hasCode:121740648
onCreate: SecondActivity TaskId: 80 hasCode:15009890
Test results:
1) MainActivity and EcondActivity are in the same task!
2) Two Second Activities are different instances.
Result analysis:
This is the same as without adding FLAG_ACTIVITY_CLEAR_TOP! This means that when two activities jump each other's android: task Affinity is the same, it will not produce any effect!
Next, take a look at the different Android: task Affinity scenarios. The code is consistent and the manifest file is modified.
File configuration
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
<activity android:name="SecondActivity"
android:taskAffinity="com.maweiqi.second"/>
Test method:
MainActivity–> SecondActivity –> MainActivity–> SecondActivity
Log Output
onCreate: MainActivity TaskId: 83 hasCode:195694529
05-18 05:38:11.757 6974-6974/com.open.android.task5 I/maweiqi: onCreate: SecondActivity TaskId: 84 hasCode:54326677
05-18 05:38:12.988 6974-6974/com.open.android.task5 I/maweiqi: onCreate: MainActivity TaskId: 84 hasCode:190178689
05-18 05:38:13.965 6974-6974/com.open.android.task5 I/maweiqi: onCreate: SecondActivity TaskId: 84 hasCode:5761837
Test results:
1) MainActivity and EcondActivity are in different task s!
2) Two Second Activities are different instances.
Result analysis:
The use of FLAG_ACTIVITY_NEW_TASK and FLAG_ACTIVITY_CLEAR_TOP is related to android: task Affinity.
In the case of using FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_CLEAR_TOP at the same time, start B with A
1) When taskAffinity of A and B is the same, adding FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_CLEAR_TOP has no effect. The effect is the same as when no additions are made!
2) When taskAffinity of A and B is different: adding FLAG_ACTIVITY_NEW_TASK|FLAG_ACTIVITY_CLEAR_TOP, the performance is the same as that of B singleTask!