Fix testAccessGravity created mock activity not being destroyed

The case android.widget.cts.TextViewTest#testAccessFreezesText will create
and destroy MockURLSpanTestActivity twice during its test. However, due to
the timing of ActivityMonitor.waitForActivityWithTimeout(), the second
MockURLSpanTestActivity would not be destroyed with the message “Duplicate
finish request for ActivityRecord{40af0798 com.android.cts.stub/android.text.style.cts.MockURLSpanTestActivity}”

Hence the remained MockURLSpanTestActivity on screen will affect followed
test case such as testAccessGravity
diff --git a/tests/tests/widget/src/android/widget/cts/TextViewTest.java b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
index dd8ea7f..bf0714c 100644
--- a/tests/tests/widget/src/android/widget/cts/TextViewTest.java
+++ b/tests/tests/widget/src/android/widget/cts/TextViewTest.java
@@ -1223,8 +1223,14 @@
             }
         });
 
-        newActivity = am.waitForActivityWithTimeout(TIMEOUT);
-        assertNotNull(newActivity);
+        Activity oldActivity = newActivity;
+        while (true) {
+            newActivity = am.waitForActivityWithTimeout(TIMEOUT);
+            assertNotNull(newActivity);
+            if (newActivity != oldActivity) {
+                break;
+            }
+        }
         newActivity.finish();
         instrumentation.removeMonitor(am);
         // the text of TextView is still there.