Dbrant has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/262844

Change subject: Fix memory issues when switching between different Fragments.
......................................................................

Fix memory issues when switching between different Fragments.

...In which I slightly modify (simplify?) the way we place and exchange our 
various
fragments inside the container view in our Activity.

This seems to stabilize memory usage when changing back and forth between 
PageFragment
and other fragments, which is the leading cause of OOM errors at the moment.

This shouldn't change any UX behavior (e.g. back button logic, etc).

Bug: T121759
Change-Id: I745a31f23030b60575cd1b071eabc49c092a1b30
---
M app/src/main/java/org/wikipedia/page/PageActivity.java
1 file changed, 12 insertions(+), 28 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/android/wikipedia 
refs/changes/44/262844/1

diff --git a/app/src/main/java/org/wikipedia/page/PageActivity.java 
b/app/src/main/java/org/wikipedia/page/PageActivity.java
index 71cd53b..1cdebad 100644
--- a/app/src/main/java/org/wikipedia/page/PageActivity.java
+++ b/app/src/main/java/org/wikipedia/page/PageActivity.java
@@ -505,18 +505,8 @@
         drawerLayout.closeDrawer(GravityCompat.START);
     }
 
-    /**
-     * Reset our fragment structure to the default, which is simply one 
PageViewFragment
-     * on the fragment stack.
-     * @param allowStateLoss Whether to allow state loss.
-     */
-    private void resetFragments(boolean allowStateLoss) {
-        while (getTopFragment() != null && !(getTopFragment() instanceof 
PageFragment)) {
-            getSupportFragmentManager().popBackStackImmediate();
-        }
-        if (getTopFragment() == null) {
-            pushFragment(new PageFragment(), allowStateLoss);
-        }
+    private void removeAllFragments() {
+        getSupportFragmentManager().popBackStackImmediate(null, 
FragmentManager.POP_BACK_STACK_INCLUSIVE);
     }
 
     /**
@@ -544,15 +534,10 @@
         if (getTopFragment() != null && (getTopFragment().getClass() == 
f.getClass())) {
             return;
         }
-        int totalFragments = 
getSupportFragmentManager().getBackStackEntryCount();
-        if (totalFragments > 0) {
-            resetFragments(allowStateLoss);
-        }
-        FragmentTransaction trans = 
getSupportFragmentManager().beginTransaction();
 
-        // do an animation on the new fragment, but only if there was a 
previous one before it.
-        trans.setCustomAnimations(R.anim.abc_fade_in, R.anim.abc_fade_out, 0, 
0);
-        trans.replace(R.id.content_fragment_container, f, "fragment_" + 
Integer.toString(totalFragments));
+        removeAllFragments();
+        FragmentTransaction trans = 
getSupportFragmentManager().beginTransaction();
+        trans.add(R.id.content_fragment_container, f);
         trans.addToBackStack(null);
         if (allowStateLoss) {
             trans.commitAllowingStateLoss();
@@ -581,8 +566,7 @@
     }
 
     public void resetAfterClearHistory() {
-        // remove all current fragments from the backstack
-        getSupportFragmentManager().popBackStackImmediate(null, 
FragmentManager.POP_BACK_STACK_INCLUSIVE);
+        removeAllFragments();
         Prefs.clearTabs();
         loadMainPageIfNoTabs();
     }
@@ -635,7 +619,8 @@
             visitInExternalBrowser(this, Uri.parse(title.getMobileUri()));
             return;
         }
-        resetFragments(allowStateLoss);
+
+        pushFragment(new PageFragment(), allowStateLoss);
 
         fragmentContainerView.post(new Runnable() {
             @Override
@@ -749,12 +734,11 @@
         if (getTopFragment() instanceof BackPressedHandler
                 && ((BackPressedHandler) getTopFragment()).onBackPressed()) {
             return;
+        } else if (!(getTopFragment() instanceof PageFragment)) {
+            pushFragment(new PageFragment(), false);
+            return;
         }
-        if (getSupportFragmentManager().getBackStackEntryCount() > 1) {
-            popFragment();
-        } else {
-            finish();
-        }
+        finish();
     }
 
     /*package*/ void showPageSavedMessage(@NonNull String title, boolean 
success) {

-- 
To view, visit https://gerrit.wikimedia.org/r/262844
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I745a31f23030b60575cd1b071eabc49c092a1b30
Gerrit-PatchSet: 1
Gerrit-Project: apps/android/wikipedia
Gerrit-Branch: master
Gerrit-Owner: Dbrant <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to