Preserve task when clearing top activities

There is no guarantee that clear-top won't finish all activities
in the task, such as the last activity is declared as default
launch mode and the intent flags don't have SINGLE_TOP, then it
will also be finished.

Otherwise when adding the new activity into the detached task,
it will cause NPE in onChildPositionChanged because its display
content is null.

Fixes: 224462770
Test: TaskTests#testPerformClearTop
Change-Id: Icfa0390a5881043bc55486347aa2854db100d171
This commit is contained in:
Riddle Hsu
2022-03-15 15:46:31 +08:00
parent 9675d3b877
commit 56ce0fa5c1
2 changed files with 18 additions and 0 deletions

View File

@@ -1623,12 +1623,16 @@ class Task extends TaskFragment {
}
ActivityRecord performClearTop(ActivityRecord newR, int launchFlags) {
// The task should be preserved for putting new activity in case the last activity is
// finished if it is normal launch mode and not single top ("clear-task-top").
mReuseTask = true;
mTaskSupervisor.beginDeferResume();
final ActivityRecord result;
try {
result = clearTopActivities(newR, launchFlags);
} finally {
mTaskSupervisor.endDeferResume();
mReuseTask = false;
}
return result;
}

View File

@@ -258,6 +258,20 @@ public class TaskTests extends WindowTestsBase {
assertNull(task1.isInTask(activity2));
}
@Test
public void testPerformClearTop() {
final Task task = createTask(mDisplayContent);
final ActivityRecord activity1 = new ActivityBuilder(mAtm).setTask(task).build();
final ActivityRecord activity2 = new ActivityBuilder(mAtm).setTask(task).build();
// Detach from process so the activities can be removed from hierarchy when finishing.
activity1.detachFromProcess();
activity2.detachFromProcess();
assertNull(task.performClearTop(activity1, 0 /* launchFlags */));
assertFalse(task.hasChild());
// In real case, the task should be preserved for adding new activity.
assertTrue(task.isAttached());
}
@Test
public void testRemoveChildForOverlayTask() {
final Task task = createTask(mDisplayContent);