Merge "DO NOT MERGE Reset top focused root task when task hidden" into rvc-qpr-dev
diff --git a/services/core/java/com/android/server/wm/Task.java b/services/core/java/com/android/server/wm/Task.java
index db3c74f..b19b8c1 100644
--- a/services/core/java/com/android/server/wm/Task.java
+++ b/services/core/java/com/android/server/wm/Task.java
@@ -4597,11 +4597,11 @@
         }
         final boolean wasHidden = isForceHidden();
         mForceHiddenFlags = newFlags;
-        if (wasHidden && isFocusableAndVisible()) {
-            // The change in force-hidden state will change visibility without triggering a stack
-            // order change, so we should reset the preferred top focusable stack to ensure it's not
-            // used if a new activity is started from this task.
-            getDisplayArea().resetPreferredTopFocusableStackIfBelow(this);
+        if (wasHidden != isForceHidden() && isTopActivityFocusable()) {
+            // The change in force-hidden state will change visibility without triggering a root
+            // task order change, so we should reset the preferred top focusable root task to ensure
+            // it's not used if a new activity is started from this task.
+            getDisplayArea().resetPreferredTopFocusableRootTaskIfNeeded(this);
         }
         return true;
     }
diff --git a/services/core/java/com/android/server/wm/TaskDisplayArea.java b/services/core/java/com/android/server/wm/TaskDisplayArea.java
index 79f3b83..676d7e5 100644
--- a/services/core/java/com/android/server/wm/TaskDisplayArea.java
+++ b/services/core/java/com/android/server/wm/TaskDisplayArea.java
@@ -773,9 +773,10 @@
         onStackOrderChanged(stack);
     }
 
-    void resetPreferredTopFocusableStackIfBelow(Task task) {
+    /** Reset the mPreferredTopFocusableRootTask if it is or below the given task. */
+    void resetPreferredTopFocusableRootTaskIfNeeded(Task task) {
         if (mPreferredTopFocusableStack != null
-                && mPreferredTopFocusableStack.compareTo(task) < 0) {
+                && mPreferredTopFocusableStack.compareTo(task) <= 0) {
             mPreferredTopFocusableStack = null;
         }
     }