[Android] Save fragments to reuse them

Geoffrey Métais git at videolan.org
Wed Dec 24 11:43:23 CET 2014


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue Dec 23 15:50:14 2014 +0100| [e34f322bbdf817a7e4017d801d19c1bea0f6e715] | committer: Geoffrey Métais

Save fragments to reuse them

To not destroy fragments, I add them to the backstack, but I don't use it.
Only to retrieve fragments by tag.
That's why we finish() on back pressed if no other action is planned.

I don't see any other solution for now, documentation explains that fragments not in backstack are destroyed once replaced.
An alternative could be to change the way fragments are handled, and don't use ft.replace() anymore.

After a few testings it seems to work pretty well, and no extra memory is needed

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=e34f322bbdf817a7e4017d801d19c1bea0f6e715
---

 vlc-android/src/org/videolan/vlc/gui/MainActivity.java  |   15 ++++++---------
 .../videolan/vlc/gui/audio/AudioBrowserFragment.java    |    9 +++++----
 2 files changed, 11 insertions(+), 13 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 8360f46..d020a25 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -259,12 +259,6 @@ public class MainActivity extends ActionBarActivity {
                     return;
 
                 if (entry.type == SidebarEntry.TYPE_FRAGMENT) {
-                /*
-                 * Clear any backstack before switching tabs. This avoids
-                 * activating an old backstack, when a user hits the back button
-                 * to quit
-                 */
-                    getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
 
                 /* Slide down the audio player */
                     slideDownAudioPlayer();
@@ -275,6 +269,7 @@ public class MainActivity extends ActionBarActivity {
                         ((IBrowser)fragment).setReadyToDisplay(false);
                     FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                     ft.replace(R.id.fragment_placeholder, fragment, entry.id);
+                    ft.addToBackStack(mCurrentFragment);
                     ft.commit();
                     supportInvalidateOptionsMenu();
                     mCurrentFragment = entry.id;
@@ -466,7 +461,7 @@ public class MainActivity extends ActionBarActivity {
 
         if (mCurrentFragment!= null) {
             // If it's the directory view, a "backpressed" action shows a parent.
-            if (mCurrentFragment.equals("directories")) {
+            if (mCurrentFragment.equals(SidebarEntry.ID_DIRECTORIES)) {
                 DirectoryViewFragment directoryView = (DirectoryViewFragment) getFragment(mCurrentFragment);
                 if (!directoryView.isRootDirectory()) {
                     directoryView.showParentDirectory();
@@ -480,12 +475,14 @@ public class MainActivity extends ActionBarActivity {
                 return;
             }
         }
-
-        super.onBackPressed();
+        finish();
     }
 
     private Fragment getFragment(String id)
     {
+        Fragment frag = getSupportFragmentManager().findFragmentByTag(id);
+        if (frag != null)
+            return frag;
         return mSidebarAdapter.fetchFragment(id);
     }
 
diff --git a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
index e71363c..5ed5097 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -55,9 +55,9 @@ import org.videolan.libvlc.Media;
 import org.videolan.vlc.MediaLibrary;
 import org.videolan.vlc.R;
 import org.videolan.vlc.audio.AudioServiceController;
-import org.videolan.vlc.interfaces.IBrowser;
 import org.videolan.vlc.gui.CommonDialogs;
 import org.videolan.vlc.gui.MainActivity;
+import org.videolan.vlc.interfaces.IBrowser;
 import org.videolan.vlc.util.AndroidDevices;
 import org.videolan.vlc.util.VLCRunnable;
 import org.videolan.vlc.util.WeakHandler;
@@ -200,10 +200,11 @@ public class AudioBrowserFragment extends Fragment implements SwipeRefreshLayout
         mFlingViewGroup.setPosition(mFlingViewPosition);
         mHeader.highlightTab(-1, mFlingViewPosition);
         mHeader.scroll(mFlingViewPosition / 3.f);
-        if (!mMediaLibrary.isWorking())
-            updateLists();
-        else
+        if (mMediaLibrary.isWorking())
             mHandler.sendEmptyMessageDelayed(MSG_LOADING, 300);
+        else if (mGenresAdapter.isEmpty() || mArtistsAdapter.isEmpty() ||
+                mAlbumsAdapter.isEmpty() || mSongsAdapter.isEmpty())
+            updateLists();
         mMediaLibrary.addUpdateHandler(mHandler);
     }
 



More information about the Android mailing list