[Android] Properly restore fragments list on device rotation

Geoffrey Métais git at videolan.org
Thu Jul 13 17:44:32 CEST 2017


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Thu Jul 13 17:43:59 2017 +0200| [d64a4229ff084ad7f03eff4625068e5ebd007378] | committer: Geoffrey Métais

Properly restore fragments list on device rotation

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

 vlc-android/src/org/videolan/vlc/gui/MainActivity.java | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 8e13d8108..cc05a5c46 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -51,6 +51,7 @@ import android.support.v7.app.ActionBarDrawerToggle;
 import android.support.v7.view.ActionMode;
 import android.support.v7.widget.SearchView;
 import android.text.TextUtils;
+import android.util.Log;
 import android.view.KeyEvent;
 import android.view.Menu;
 import android.view.MenuInflater;
@@ -113,7 +114,7 @@ public class MainActivity extends AudioPlayerContainerActivity implements Filter
     private ActionBarDrawerToggle mDrawerToggle;
 
     private int mCurrentFragmentId;
-    private Fragment mCurrentFragment;
+    private Fragment mCurrentFragment = null;
     private final SimpleArrayMap<String, WeakReference<Fragment>> mFragmentsStack = new SimpleArrayMap<>();
 
     private boolean mScanNeeded = false;
@@ -150,10 +151,17 @@ public class MainActivity extends AudioPlayerContainerActivity implements Filter
         initAudioPlayerContainerActivity();
 
         if (savedInstanceState != null) {
+            //Restore fragments stack
+            for (Fragment fragment : getSupportFragmentManager().getFragments())
+                mFragmentsStack.put(fragment.getTag(), new WeakReference<>(fragment));
+
             mCurrentFragmentId = savedInstanceState.getInt("current");
-            mCurrentFragment = getCurrentFragment();
-            if (mCurrentFragmentId > 0)
+            if (mCurrentFragmentId > 0) {
                 mNavigationView.setCheckedItem(mCurrentFragmentId);
+                String tag = getTag(mCurrentFragmentId);
+                if (mFragmentsStack.containsKey(tag))
+                    mCurrentFragment = mFragmentsStack.get(tag).get();
+            }
         }
 
         /* Set up the action bar */
@@ -789,7 +797,6 @@ public class MainActivity extends AudioPlayerContainerActivity implements Filter
                 }
             }
 
-            String tag = getTag(id);
             switch (id){
                 case R.id.nav_about:
                     showSecondaryFragment(SecondaryActivity.ABOUT);



More information about the Android mailing list