Revert "Allow transitioning through lifecycle when finished"

This reverts commit 76ea768bd3efa8800400769b5e4bef45ef2561ea.

Reason for revert: b/127738408

Change-Id: I09eb93a255310c3749d25d31db77126c6133aa0c
(cherry picked from commit c10482ff3c36ec5902f2b1f4163ca7c89a0fa6c2)
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index c0a702f..08239a1 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -3115,6 +3115,10 @@
         if (!r.stopped) {
             throw new IllegalStateException("Can't start activity that is not stopped.");
         }
+        if (r.activity.mFinished) {
+            // TODO(lifecycler): How can this happen?
+            return;
+        }
 
         // Start
         activity.performStart("handleStartActivity");
@@ -3237,8 +3241,6 @@
             if (!r.activity.mFinished && pendingActions != null) {
                 pendingActions.setOldState(r.state);
                 pendingActions.setRestoreInstanceState(true);
-            }
-            if (pendingActions != null) {
                 pendingActions.setCallOnPostCreate(true);
             }
         } else {
@@ -3940,7 +3942,7 @@
         if (localLOGV) {
             Slog.v(TAG, "Performing resume of " + r + " finished=" + r.activity.mFinished);
         }
-        if (r == null) {
+        if (r == null || r.activity.mFinished) {
             return null;
         }
         if (r.getLifecycleState() == ON_RESUME) {
@@ -4210,6 +4212,12 @@
     private Bundle performPauseActivity(ActivityClientRecord r, boolean finished, String reason,
             PendingTransactionActions pendingActions) {
         if (r.paused) {
+            if (r.activity.mFinished) {
+                // If we are finishing, we won't call onResume() in certain cases.
+                // So here we likewise don't want to call onPause() if the activity
+                // isn't resumed.
+                return null;
+            }
             RuntimeException e = new RuntimeException(
                     "Performing pause of activity that is not resumed: "
                     + r.intent.getComponent().toShortString());
@@ -4329,13 +4337,20 @@
             boolean saveState, boolean finalStateRequest, String reason) {
         if (localLOGV) Slog.v(TAG, "Performing stop of " + r);
         if (r != null) {
-            if (!keepShown && r.stopped && !finalStateRequest) {
-                // Double stop request is possible if activity receives 'sleep' followed by 'stop'.
-                final RuntimeException e = new RuntimeException(
-                        "Performing stop of activity that is already stopped: "
-                                + r.intent.getComponent().toShortString());
-                Slog.e(TAG, e.getMessage(), e);
-                Slog.e(TAG, r.getStateString());
+            if (!keepShown && r.stopped) {
+                if (r.activity.mFinished) {
+                    // If we are finishing, we won't call onResume() in certain
+                    // cases.  So here we likewise don't want to call onStop()
+                    // if the activity isn't resumed.
+                    return;
+                }
+                if (!finalStateRequest) {
+                    final RuntimeException e = new RuntimeException(
+                            "Performing stop of activity that is already stopped: "
+                                    + r.intent.getComponent().toShortString());
+                    Slog.e(TAG, e.getMessage(), e);
+                    Slog.e(TAG, r.getStateString());
+                }
             }
 
             // One must first be paused before stopped...