[Android] Add/remove fragments instead of replace

Geoffrey Métais git at videolan.org
Tue Dec 20 11:12:40 CET 2016


vlc-android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Dec 20 09:22:00 2016 +0100| [6962423ef17fbaf4ee0edf3f04349ea7801b3ded] | committer: Geoffrey Métais

Add/remove fragments instead of replace

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

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

diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 2d56f68..6d3f645 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -386,11 +386,13 @@ public class MainActivity extends AudioPlayerContainerActivity implements Device
          * (i.e. tracks) and replace it with a blank screen. (stuck menu bug)
          */
         if (current == null) {
+            String tag = getTag(mCurrentFragmentId);
             mNavigationView.setCheckedItem(mCurrentFragmentId);
             Fragment ff = getFragment(mCurrentFragmentId);
-            FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
-            ft.replace(R.id.fragment_placeholder, ff, getTag(mCurrentFragmentId));
-            ft.commit();
+            getSupportFragmentManager().beginTransaction()
+                .add(R.id.fragment_placeholder, ff, tag)
+                .addToBackStack(tag)
+                .commit();
         }
     }
 
@@ -454,6 +456,11 @@ public class MainActivity extends AudioPlayerContainerActivity implements Device
         Fragment frag = getSupportFragmentManager().findFragmentByTag(getTag(id));
         if (frag != null)
             return frag;
+        return getNewFragment(id);
+    }
+
+    @NonNull
+    private Fragment getNewFragment(int id) {
         switch (id) {
             case R.id.nav_audio:
                 return new AudioBrowserFragment();
@@ -945,10 +952,11 @@ public class MainActivity extends AudioPlayerContainerActivity implements Device
 
                 /* Switch the fragment */
                     Fragment fragment = getFragment(id);
-                    FragmentTransaction ft = fm.beginTransaction();
-                    ft.replace(R.id.fragment_placeholder, fragment, tag);
-                    ft.addToBackStack(getTag(mCurrentFragmentId));
-                    ft.commit();
+                    fm.beginTransaction()
+                        .remove(current)
+                        .add(R.id.fragment_placeholder, fragment, tag)
+                        .addToBackStack(tag)
+                        .commit();
                     mCurrentFragmentId = id;
             }
         }



More information about the Android mailing list