[Android] Main activity: try to make the fragment behavior coherent

Adrien Maglo git at videolan.org
Thu Jan 30 18:49:17 CET 2014


vlc-ports/android | branch: master | Adrien Maglo <magsoft at videolan.org> | Thu Jan 30 18:45:37 2014 +0100| [88657c1e31642dd25ccd18f7634dd60af4daf79e] | committer: Adrien Maglo

Main activity: try to make the fragment behavior coherent

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

 .../src/org/videolan/vlc/gui/CommonDialogs.java    |    2 +-
 .../src/org/videolan/vlc/gui/MainActivity.java     |   64 ++++++++++++++------
 .../src/org/videolan/vlc/gui/SidebarAdapter.java   |   11 +---
 .../vlc/gui/audio/AudioBrowserFragment.java        |    4 +-
 4 files changed, 51 insertions(+), 30 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java b/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java
index 449a73a..59a41ff 100644
--- a/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java
+++ b/vlc-android/src/org/videolan/vlc/gui/CommonDialogs.java
@@ -156,7 +156,7 @@ public class CommonDialogs {
             equalizer.setOnClickListener(new View.OnClickListener() {
                 @Override
                 public void onClick(View v) {
-                    activity.showNewFragment("equalizer");
+                    activity.showSecondaryFragment("equalizer");
                     dialog.dismiss();
                 }
             });
diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index 36a537f..84b0f45 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -20,6 +20,10 @@
 
 package org.videolan.vlc.gui;
 
+import java.util.Arrays;
+import java.util.HashMap;
+import java.util.List;
+
 import org.videolan.libvlc.LibVlcException;
 import org.videolan.libvlc.LibVlcUtil;
 import org.videolan.vlc.AudioService;
@@ -30,7 +34,9 @@ import org.videolan.vlc.Util;
 import org.videolan.vlc.VLCCallbackTask;
 import org.videolan.vlc.WeakHandler;
 import org.videolan.vlc.gui.SidebarAdapter.SidebarEntry;
+import org.videolan.vlc.gui.audio.AudioAlbumsSongsFragment;
 import org.videolan.vlc.gui.audio.AudioPlayer;
+import org.videolan.vlc.gui.audio.EqualizerFragment;
 import org.videolan.vlc.gui.video.VideoListAdapter;
 import org.videolan.vlc.interfaces.ISortable;
 import org.videolan.vlc.widget.SlidingPaneLayout;
@@ -108,6 +114,8 @@ public class MainActivity extends SherlockFragmentActivity {
     private View mAudioPlayerFilling;
     private String mCurrentFragment;
     private String mPreviousFragment;
+    private List<String> secondaryFragments = Arrays.asList("albumsSongs", "equalizer");
+    private HashMap<String, Fragment> mSecondaryFragments = new HashMap<String, Fragment>();
 
     private SharedPreferences mSettings;
 
@@ -375,10 +383,6 @@ public class MainActivity extends SherlockFragmentActivity {
         MediaLibrary.getInstance(this).stop();
         /* Save the tab status in pref */
         SharedPreferences.Editor editor = getSharedPreferences("MainActivity", MODE_PRIVATE).edit();
-        /* Do not save the albums songs fragment as the current fragment. */
-        if (mCurrentFragment.equals("albumsSongs")
-            || mCurrentFragment.equals("equalizer"))
-            mCurrentFragment = "audio";
         editor.putString("fragment", mCurrentFragment);
         editor.commit();
 
@@ -424,9 +428,8 @@ public class MainActivity extends SherlockFragmentActivity {
         }
 
         // If it's the albums songs fragment, we leave it.
-        if (mCurrentFragment.equals("albumsSongs")
-            || mCurrentFragment.equals("equalizer")) {
-            popFragmentBackStack();
+        if (secondaryFragments.contains(mCurrentFragment)) {
+            popSecondaryFragment();
             return;
         }
 
@@ -464,9 +467,33 @@ public class MainActivity extends SherlockFragmentActivity {
     }
 
     /**
-     * Show a new fragment.
+     * Fetch a secondary fragment.
+     * @param id the fragment id
+     * @return the fragment.
+     */
+    public Fragment fetchSecondaryFragment(String id) {
+        if (mSecondaryFragments.containsKey(id)
+            && mSecondaryFragments.get(id) != null)
+            return mSecondaryFragments.get(id);
+
+        Fragment f;
+        if (id.equals("albumsSongs")) {
+            f = new AudioAlbumsSongsFragment();
+        } else if(id.equals("equalizer")) {
+            f = new EqualizerFragment();
+        }
+        else {
+            throw new IllegalArgumentException("Wrong fragment id.");
+        }
+        f.setRetainInstance(true);
+        mSecondaryFragments.put(id, f);
+        return f;
+    }
+
+    /**
+     * Show a secondary fragment.
      */
-    public Fragment showNewFragment(String fragmentTag) {
+    public Fragment showSecondaryFragment(String fragmentTag) {
         // Slide down the audio player if needed.
         slideDownAudioPlayer();
 
@@ -474,19 +501,21 @@ public class MainActivity extends SherlockFragmentActivity {
         if (mCurrentFragment.equals(fragmentTag))
             return null;
 
-        mPreviousFragment = mCurrentFragment;
+        if (!secondaryFragments.contains(mCurrentFragment))
+            mPreviousFragment = mCurrentFragment;
+
         mCurrentFragment = fragmentTag;
-        Fragment frag = getFragment(mCurrentFragment);
+        Fragment frag = fetchSecondaryFragment(mCurrentFragment);
         ShowFragment(this, mCurrentFragment, frag);
         return frag;
     }
 
     /**
-     * Hide the albums songs fragment.
+     * Hide the current secondary fragment.
      */
-    public void popFragmentBackStack() {
+    public void popSecondaryFragment() {
         FragmentManager fragmentManager = getSupportFragmentManager();
-        fragmentManager.popBackStack();
+        fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
         mCurrentFragment = mPreviousFragment;
     }
 
@@ -559,7 +588,7 @@ public class MainActivity extends SherlockFragmentActivity {
                 startActivityForResult(intent, ACTIVITY_RESULT_PREFERENCES);
                 break;
             case R.id.ml_menu_equalizer:
-                showNewFragment("equalizer");
+                showSecondaryFragment("equalizer");
                 break;
             // Refresh
             case R.id.ml_menu_refresh:
@@ -587,9 +616,8 @@ public class MainActivity extends SherlockFragmentActivity {
                 break;
             case android.R.id.home:
                 // If it's the albums songs view, a "backpressed" action shows .
-                if (mCurrentFragment.equals("albumsSongs")
-                    || mCurrentFragment.equals("equalizer")) {
-                    popFragmentBackStack();
+                if (secondaryFragments.contains(mCurrentFragment)) {
+                    popSecondaryFragment();
                     break;
                 }
                 /* Toggle the sidebar */
diff --git a/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java b/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
index 395bfeb..7800de0 100644
--- a/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
@@ -27,9 +27,7 @@ import java.util.List;
 import org.videolan.vlc.R;
 import org.videolan.vlc.Util;
 import org.videolan.vlc.VLCApplication;
-import org.videolan.vlc.gui.audio.AudioAlbumsSongsFragment;
 import org.videolan.vlc.gui.audio.AudioBrowserFragment;
-import org.videolan.vlc.gui.audio.EqualizerFragment;
 import org.videolan.vlc.gui.video.VideoGridFragment;
 
 import android.graphics.drawable.Drawable;
@@ -131,14 +129,9 @@ public class SidebarAdapter extends BaseAdapter {
             f = new DirectoryViewFragment();
         } else if(id.equals("history")) {
             f = new HistoryFragment();
-        } else if(id.equals("albumsSongs")) {
-            f = new AudioAlbumsSongsFragment();
-        } else if(id.equals("equalizer")) {
-            f = new EqualizerFragment();
         }
-        else
-        { /* TODO */
-            f = new AboutLicenceFragment();
+        else {
+            throw new IllegalArgumentException("Wrong fragment id.");
         }
         f.setRetainInstance(true);
         mFragments.put(id, f);
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 4137ce3..15f8ae6 100644
--- a/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/audio/AudioBrowserFragment.java
@@ -178,7 +178,7 @@ public class AudioBrowserFragment extends SherlockFragment {
         public void onItemClick(AdapterView<?> av, View v, int p, long id) {
             ArrayList<Media> mediaList = mArtistsAdapter.getMedia(p);
             MainActivity activity = (MainActivity)getActivity();
-            AudioAlbumsSongsFragment frag = (AudioAlbumsSongsFragment)activity.showNewFragment("albumsSongs");
+            AudioAlbumsSongsFragment frag = (AudioAlbumsSongsFragment)activity.showSecondaryFragment("albumsSongs");
             frag.setMediaList(mediaList, mediaList.get(0).getArtist());
         }
     };
@@ -196,7 +196,7 @@ public class AudioBrowserFragment extends SherlockFragment {
         public void onItemClick(AdapterView<?> av, View v, int p, long id) {
             ArrayList<Media> mediaList = mGenresAdapter.getMedia(p);
             MainActivity activity = (MainActivity)getActivity();
-            AudioAlbumsSongsFragment frag = (AudioAlbumsSongsFragment)activity.showNewFragment("albumsSongs");
+            AudioAlbumsSongsFragment frag = (AudioAlbumsSongsFragment)activity.showSecondaryFragment("albumsSongs");
             frag.setMediaList(mediaList, mediaList.get(0).getGenre());
         }
     };



More information about the Android mailing list