[Android] Improve fragments stack restoration

Geoffrey Métais git at videolan.org
Thu Sep 21 11:30:59 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Sep 21 11:30:00 2017 +0200| [152865c5b4d3924d9f94c59aabc8c58ea149aa9f] | committer: Geoffrey Métais

Improve fragments stack restoration

> https://code.videolan.org/videolan/vlc-android/commit/152865c5b4d3924d9f94c59aabc8c58ea149aa9f
---

 .../src/org/videolan/vlc/gui/MainActivity.java     | 41 +++++++++++++---------
 1 file changed, 24 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index a1afee83c..7c65fb10a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -131,21 +131,7 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
             final FragmentManager fm = getSupportFragmentManager();
             mCurrentFragment = fm.getFragment(savedInstanceState, "current_fragment");
             //Restore fragments stack
-            final List<Fragment> fragments = fm.getFragments();
-            if (fragments != null) {
-                final FragmentTransaction ft =  fm.beginTransaction();
-                for (Fragment fragment : fragments)
-                    if (fragment != null) {
-                        if (fragment instanceof ExtensionBrowser) {
-                            ft.remove(fragment);
-                        } else if ((fragment instanceof MediaBrowserFragment)) {
-                            mFragmentsStack.put(fragment.getTag(), new WeakReference<>(fragment));
-                            if (fragment != mCurrentFragment)
-                                ft.hide(fragment);
-                        }
-                    }
-                ft.commit();
-            }
+            restoreFragmentsStack(savedInstanceState, fm);
             mCurrentFragmentId = savedInstanceState.getInt("current", mSettings.getInt("fragment_id", R.id.nav_video));
         } else
             reloadPreferences();
@@ -200,6 +186,25 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
         mMediaLibrary = VLCApplication.getMLInstance();
     }
 
+    private void restoreFragmentsStack(Bundle savedInstanceState, FragmentManager fm) {
+        final List<Fragment> fragments = fm.getFragments();
+        if (fragments != null) {
+            final FragmentTransaction ft =  fm.beginTransaction();
+            final Fragment displayed = fm.getFragment(savedInstanceState, "current_fragment_visible");
+            for (Fragment fragment : fragments)
+                if (fragment != null) {
+                    if (fragment instanceof ExtensionBrowser) {
+                        ft.remove(fragment);
+                    } else if ((fragment instanceof MediaBrowserFragment)) {
+                        mFragmentsStack.put(fragment.getTag(), new WeakReference<>(fragment));
+                        if (fragment != displayed)
+                            ft.hide(fragment);
+                    }
+                }
+            ft.commit();
+        }
+    }
+
     private void setupNavigationView() {
         mNavigationView = (NavigationView) findViewById(R.id.navigation);
         if (TextUtils.equals(BuildConfig.FLAVOR_target, "chrome")) {
@@ -314,8 +319,10 @@ public class MainActivity extends ContentActivity implements FilterQueryProvider
     protected void onSaveInstanceState(Bundle outState) {
         if (mCurrentFragment instanceof ExtensionBrowser)
             mCurrentFragment = null;
-        else
-            getSupportFragmentManager().putFragment(outState, "current_fragment", getCurrentFragment());
+        else {
+            getSupportFragmentManager().putFragment(outState, "current_fragment", mCurrentFragment);
+            getSupportFragmentManager().putFragment(outState, "current_fragment_visible", getCurrentFragment());
+        }
         super.onSaveInstanceState(outState);
         outState.putInt("current", mCurrentFragmentId);
     }



More information about the Android mailing list