Bug 1437382 - Part 2 - Allow triggering application-background earlier when possible. r?jchen draft
authorJan Henning <jh+bugzilla@buttercookie.de>
Sun, 11 Feb 2018 17:14:54 +0100
changeset 759442 238e2d59be7746fe7ffc5a1556ac3f3befe84fbf
parent 759441 7dcd3789cb9b6a2f1c0d177b331c0fda704396b6
child 759443 507ee0985443dbcb10212b6f30d6f88ec2e4e630
child 760103 ec5f55a68fbdfe80b987f8b7027ffb1ad7210bfb
push id100352
push usermozilla@buttercookie.de
push dateSat, 24 Feb 2018 11:43:59 +0000
reviewersjchen
bugs1437382
milestone60.0a1
Bug 1437382 - Part 2 - Allow triggering application-background earlier when possible. r?jchen If onActivitySaveInstanceState is called, this normally happens just before onActivityStopped, which means we can attempt to use the same logic currently used for onActivityStopped to determine whether the whole app is going into the background or we're doing just an internal activity switch. This allows us to trigger our Gecko onPause/"application-background" handling *before* e.g. GeckoApp's onSaveInstanceState handling, meaning we still have time to update the private browsing data held in GeckoApp before it is passed off to the OS. MozReview-Commit-ID: 5TZ6uX0gyz1
mobile/android/base/java/org/mozilla/gecko/GeckoActivityMonitor.java
--- a/mobile/android/base/java/org/mozilla/gecko/GeckoActivityMonitor.java
+++ b/mobile/android/base/java/org/mozilla/gecko/GeckoActivityMonitor.java
@@ -27,16 +27,27 @@ public class GeckoActivityMonitor implem
 
     private void updateActivity(final Activity activity) {
         if (currentActivity.get() == null) {
             ((GeckoApplication) activity.getApplication()).onApplicationForeground();
         }
         currentActivity = new WeakReference<>(activity);
     }
 
+    private void checkAppGoingIntoBackground(final Activity activity) {
+        // For the previous activity, this is called after onStart/onResume for the
+        // new/resumed activity, so if we're switching activities within our app,
+        // currentActivity should already refer to the next activity at this point.
+        // If it doesn't, it means we've been backgrounded.
+        if (currentActivity.get() == activity) {
+            currentActivity.clear();
+            ((GeckoApplication) activity.getApplication()).onApplicationBackground();
+        }
+    }
+
     public Activity getCurrentActivity() {
         return currentActivity.get();
     }
 
     public synchronized void initialize(final GeckoApplication app) {
         app.registerActivityLifecycleCallbacks(this);
     }
 
@@ -52,26 +63,20 @@ public class GeckoActivityMonitor implem
     public void onActivityResumed(Activity activity) {
         updateActivity(activity);
     }
 
     @Override
     public void onActivityPaused(Activity activity) { }
 
     @Override
-    public void onActivityStopped(Activity activity) {
-        // onStop for the previous activity is called after onStart/onResume for
-        // the new/resumed activity, so if we're switching activities within our
-        // app, currentActivity should already refer to the next activity at
-        // this point.
-        // If it doesn't, it means we've been backgrounded.
-        if (currentActivity.get() == activity) {
-            currentActivity.clear();
-            ((GeckoApplication) activity.getApplication()).onApplicationBackground();
-        }
+    public void onActivitySaveInstanceState(Activity activity, Bundle outState) {
+        checkAppGoingIntoBackground(activity);
     }
 
     @Override
-    public void onActivitySaveInstanceState(Activity activity, Bundle outState) { }
+    public void onActivityStopped(Activity activity) {
+        checkAppGoingIntoBackground(activity);
+    }
 
     @Override
     public void onActivityDestroyed(Activity activity) { }
 }