[Android] Do not add a fragment to a view, if we do not mean to display it

Sébastien Toque git at videolan.org
Mon Oct 1 02:17:04 CEST 2012


vlc-ports/android | branch: master | Sébastien Toque <xilasz at gmail.com> | Mon Oct  1 02:14:20 2012 +0200| [59dbcf3a01a7bd75400031baf9a98a055562976a] | committer: Sébastien Toque

Do not add a fragment to a view, if we do not mean to display it

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

 .../src/org/videolan/vlc/gui/MainActivity.java     |   31 ++++++++++++++------
 .../src/org/videolan/vlc/gui/SidebarAdapter.java   |    8 +----
 2 files changed, 23 insertions(+), 16 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
index f95f230..ca6d60e 100644
--- a/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/MainActivity.java
@@ -128,7 +128,7 @@ public class MainActivity extends SherlockFragmentActivity {
         View sidebar = LayoutInflater.from(this).inflate(R.layout.sidebar, null);
         final ListView listView = (ListView)sidebar.findViewById(android.R.id.list);
         listView.setFooterDividersEnabled(true);
-        mSidebarAdapter = new SidebarAdapter(getSupportFragmentManager());
+        mSidebarAdapter = new SidebarAdapter();
         listView.setAdapter(mSidebarAdapter);
         mMenu.setMenu(sidebar);
 
@@ -213,7 +213,7 @@ public class MainActivity extends SherlockFragmentActivity {
                 }
                 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
                 ft.detach(current);
-                ft.attach(mSidebarAdapter.getFragment(entry.id));
+                ft.attach(getFragment(entry.id));
                 ft.commit();
                 mCurrentFragment = entry.id;
                 mMenu.showAbove();
@@ -280,10 +280,6 @@ public class MainActivity extends SherlockFragmentActivity {
         AudioServiceController.getInstance().bindAudioService(this);
         Boolean startFromNotification = getIntent().hasExtra(AudioService.START_FROM_NOTIFICATION);
 
-        /* Start the thumbnailer */
-        VideoListFragment f = (VideoListFragment)mSidebarAdapter.getFragment("video");
-        mThumbnailerManager.start(f);
-
         /* Restore last view */
         Fragment current = getSupportFragmentManager()
                 .findFragmentById(R.id.fragment_placeholder);
@@ -304,7 +300,7 @@ public class MainActivity extends SherlockFragmentActivity {
          */
         if(found) {
             FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
-            ft.replace(R.id.fragment_placeholder, mSidebarAdapter.getFragment(mCurrentFragment));
+            ft.replace(R.id.fragment_placeholder, getFragment(mCurrentFragment));
             ft.commit();
         }
 
@@ -357,6 +353,23 @@ public class MainActivity extends SherlockFragmentActivity {
         }
     }
 
+    public Fragment getFragment(String id)
+    {
+        Fragment fragment = mSidebarAdapter.getFragment(id);
+
+        if (!fragment.isAdded())
+            getSupportFragmentManager()
+                .beginTransaction()
+                .add(R.id.fragment_placeholder, fragment, id)
+                .commitAllowingStateLoss();
+
+        /* Start the thumbnailer */
+        if (id.equals("video"))
+            mThumbnailerManager.start((VideoListFragment)fragment);
+
+        return fragment;
+    }
+
     /** Create menu from XML
      */
     @Override
@@ -418,11 +431,11 @@ public class MainActivity extends SherlockFragmentActivity {
             case R.id.ml_menu_refresh:
                 // TODO: factor this into each fragment
                 if(mCurrentFragment.equals("directories")) {
-                    DirectoryViewFragment directoryView = (DirectoryViewFragment) mSidebarAdapter.getFragment(mCurrentFragment);
+                    DirectoryViewFragment directoryView = (DirectoryViewFragment) getFragment(mCurrentFragment);
                     directoryView.refresh();
                 }
                 else if(mCurrentFragment.equals("history"))
-                    ((HistoryFragment)mSidebarAdapter.getFragment(mCurrentFragment)).refresh();
+                    ((HistoryFragment) getFragment(mCurrentFragment)).refresh();
                 else
                     MediaLibrary.getInstance(this).loadMediaItems(this);
                 break;
diff --git a/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java b/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
index 5522733..3e69047 100644
--- a/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/SidebarAdapter.java
@@ -32,7 +32,6 @@ import org.videolan.vlc.gui.video.VideoListFragment;
 
 import android.graphics.drawable.Drawable;
 import android.support.v4.app.Fragment;
-import android.support.v4.app.FragmentManager;
 import android.view.LayoutInflater;
 import android.view.View;
 import android.view.ViewGroup;
@@ -61,7 +60,6 @@ public class SidebarAdapter extends BaseAdapter {
     }
 
     private LayoutInflater mInflater;
-    private FragmentManager mFragmentManager;
     static final List<SidebarEntry> entries;
     private HashMap<String, Fragment> mFragments;
 
@@ -77,9 +75,8 @@ public class SidebarAdapter extends BaseAdapter {
         entries = Arrays.asList(entries2);
     }
 
-    public SidebarAdapter(FragmentManager fm) {
+    public SidebarAdapter() {
         mInflater = LayoutInflater.from(VLCApplication.getAppContext());
-        mFragmentManager = fm;
         mFragments = new HashMap<String, Fragment>(entries.size());
     }
 
@@ -134,9 +131,6 @@ public class SidebarAdapter extends BaseAdapter {
             f = new AboutLicenceFragment();
         }
         f.setRetainInstance(true);
-        mFragmentManager.beginTransaction()
-            .add(R.id.fragment_placeholder, f, id)
-            .commitAllowingStateLoss();
         mFragments.put(id, f);
         return f;
     }



More information about the Android mailing list