Persist TaskRecord before calling performClearTaskLocked.

A recent change persisted this record with a member variable that
could be cleared as a result of performClearTasklocked. This
changelist resolves this by using a local variable to store the
reference until after the method call.

Change-Id: I6d5d42e06afdbf6e8ff960fcfae71c80abd891f4
Fixes: 36001400
Test: adb shell am instrument -w -e class com.google.android.setupwizard.tests.activity.SetupWizardExitActivityTest com.google.android.setupwizard.tests.activity com.google.android.setupwizard.tests.activity/.SetupWizardTestRunner
diff --git a/services/core/java/com/android/server/am/ActivityStarter.java b/services/core/java/com/android/server/am/ActivityStarter.java
index 33889ba..2bbfc21 100644
--- a/services/core/java/com/android/server/am/ActivityStarter.java
+++ b/services/core/java/com/android/server/am/ActivityStarter.java
@@ -1562,9 +1562,14 @@
             // activity. Well that should not be too hard...
             // Note: we must persist the {@link TaskRecord} first as intentActivity could be
             // removed from calling performClearTaskLocked (For example, if it is being brought out
-            // of history or if it is finished immediately), thus disassociating the task.
-            mReuseTask = intentActivity.task;
-            mReuseTask.performClearTaskLocked();
+            // of history or if it is finished immediately), thus disassociating the task. Also note
+            // that mReuseTask is reset as a result of {@link TaskRecord#performClearTaskLocked}
+            // launching another activity.
+            // TODO(b/36119896):  We shouldn't trigger activity launches in this path since we are
+            // already launching one.
+            final TaskRecord task = intentActivity.task;
+            task.performClearTaskLocked();
+            mReuseTask = task;
             mReuseTask.setIntent(mStartActivity);
 
             // When we clear the task - focus will be adjusted, which will bring another task