Bug 1579628 - Avoid Fragment initializations if parent is finishing(). r=VladBaicu, a=RyanVM
authorPetru Lingurar <petru.lingurar@softvision.ro>
Wed, 11 Dec 2019 08:26:37 +0000
changeset 524413 a9808a2e5a6e1847ee551b174e462885030174b4
parent 524412 49285dbee11ec29eb08508485714b6230f233d9e
child 524414 35aa29266d1141a5e685f0476fcc76b9c392945d
push id765
push userryanvm@gmail.com
push dateTue, 14 Jan 2020 22:56:39 +0000
treeherdermozilla-esr68@3b3f5444c547 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersVladBaicu, RyanVM
bugs1579628
milestone68.5.0
Bug 1579628 - Avoid Fragment initializations if parent is finishing(). r=VladBaicu, a=RyanVM The code in onCreate was already protected by this check but the other lifecycle methods were not. As a speculative fix for a situation in which variables which should have been initialized in onCreate() are null later we also introduce the guard for if the parent is finishing in other parts of the code. Differential Revision: https://phabricator.services.mozilla.com/D56707
mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
--- a/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
+++ b/mobile/android/base/java/org/mozilla/gecko/home/CombinedHistoryPanel.java
@@ -112,18 +112,17 @@ public class CombinedHistoryPanel extend
     @Override
     public void onCreate(Bundle savedInstance) {
         super.onCreate(savedInstance);
 
         // BrowserApp's super.onCreate() as a FragmentActivity would dispatchCreate()
         // for all fragments in it's savedInstanceState. This Fragment will be created.
         // When BrowserApp does not complete it's onCreate() - like when finishing early and restarting
         // our onCreate would try to access not yet initialized resources and would get a NPE.
-        final FragmentActivity parent = getActivity();
-        if (parent != null && parent.isFinishing()) {
+        if (isParentFinishing()) {
             return;
         }
 
         int cachedRecentTabsCount = 0;
         if (mPanelStateChangeListener != null ) {
             cachedRecentTabsCount = mPanelStateChangeListener.getCachedRecentTabsCount();
         }
         mHistoryAdapter = new CombinedHistoryAdapter(getResources(), cachedRecentTabsCount);
@@ -147,16 +146,23 @@ public class CombinedHistoryPanel extend
         return inflater.inflate(R.layout.home_combined_history_panel, container, false);
     }
 
     @UiThread
     @Override
     public void onViewCreated(View view, Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
 
+        // No need for potentially heavy initializations if we are to be destroyed soon.
+        // Also avoid potential issues stemming from the code in onCreate() already being short-circuited
+        // by this same check.
+        if (isParentFinishing()) {
+            return;
+        }
+
         mRecyclerView = (CombinedHistoryRecyclerView) view.findViewById(R.id.combined_recycler_view);
         setUpRecyclerView();
 
         mRefreshLayout = (SwipeRefreshLayout) view.findViewById(R.id.refresh_layout);
         setUpRefreshLayout();
 
         mClientsEmptyView = view.findViewById(R.id.home_clients_empty_view);
         mHistoryEmptyView = view.findViewById(R.id.home_history_empty_view);
@@ -685,9 +691,14 @@ public class CombinedHistoryPanel extend
     public void onDestroy() {
         super.onDestroy();
 
         if (mSyncStatusListener != null) {
             FirefoxAccounts.removeSyncStatusListener(mSyncStatusListener);
             mSyncStatusListener = null;
         }
     }
+
+    private boolean isParentFinishing() {
+        final FragmentActivity parent = getActivity();
+        return parent != null && parent.isFinishing();
+    }
 }