[Android] [PATCH 3/3] Keep search filter on screen rotation

Romain Vimont rom at rom1v.com
Tue Nov 7 18:34:47 CET 2017


Save and restore the search query state along with the activity.

Fixes <https://code.videolan.org/videolan/vlc-android/issues/404>
---
 .../src/org/videolan/vlc/gui/ContentActivity.java  | 24 ++++++++++++++++++++++
 .../videolan/vlc/gui/video/VideoGridFragment.java  |  2 +-
 .../videolan/vlc/gui/video/VideoListAdapter.java   |  4 ++++
 3 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java b/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
index ccefd9771..4787c7b55 100644
--- a/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/ContentActivity.java
@@ -26,6 +26,7 @@ package org.videolan.vlc.gui;
 
 import android.app.SearchManager;
 import android.content.Intent;
+import android.os.Bundle;
 import android.support.v4.app.Fragment;
 import android.support.v4.view.MenuItemCompat;
 import android.support.v7.widget.SearchView;
@@ -41,8 +42,25 @@ import org.videolan.vlc.interfaces.Filterable;
 public class ContentActivity extends AudioPlayerContainerActivity implements SearchView.OnQueryTextListener, MenuItemCompat.OnActionExpandListener {
     public static final String TAG = "VLC/ContentActivity";
 
+    private static final String KEY_SEARCH_QUERY = "org.videolan.vlc.SEARCH_QUERY";
+
     protected Menu mMenu;
     private SearchView mSearchView;
+    private CharSequence mSavedSearchQuery;
+
+    @Override
+    protected void onSaveInstanceState(Bundle outState) {
+        super.onSaveInstanceState(outState);
+        if (mSearchView != null) {
+            outState.putCharSequence(KEY_SEARCH_QUERY, mSearchView.getQuery());
+        }
+    }
+
+    @Override
+    protected void onRestoreInstanceState(Bundle savedInstanceState) {
+        super.onRestoreInstanceState(savedInstanceState);
+        mSavedSearchQuery = savedInstanceState.getCharSequence(KEY_SEARCH_QUERY);
+    }
 
     @Override
     public boolean onCreateOptionsMenu(Menu menu) {
@@ -60,6 +78,12 @@ public class ContentActivity extends AudioPlayerContainerActivity implements Sea
             mSearchView.setQueryHint(getString(R.string.search_list_hint));
             mSearchView.setOnQueryTextListener(this);
             MenuItemCompat.setOnActionExpandListener(searchItem, this);
+            if (mSavedSearchQuery != null) {
+                MenuItemCompat.expandActionView(searchItem);
+                mSearchView.setQuery(mSavedSearchQuery, false);
+                mSavedSearchQuery = null;
+                setSearchVisibility(true);
+            }
         }
         else
             menu.findItem(R.id.ml_menu_filter).setVisible(false);
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
index c61401895..418435ab8 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoGridFragment.java
@@ -178,7 +178,7 @@ public class VideoGridFragment extends SortableFragment<VideoListAdapter> implem
     public void onSaveInstanceState(Bundle outState) {
         super.onSaveInstanceState(outState);
         outState.putString(KEY_GROUP, mGroup);
-        VLCApplication.storeData("list"+getTitle(), mAdapter.getAll());
+        VLCApplication.storeData("list"+getTitle(), mAdapter.getUnfiltered());
     }
 
     protected void onMedialibraryReady() {
diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
index 33acf9729..d3ba794cf 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoListAdapter.java
@@ -173,6 +173,10 @@ public class VideoListAdapter extends SortableAdapter<MediaWrapper, VideoListAda
         return mDataset;
     }
 
+    public List<MediaWrapper> getUnfiltered() {
+        return mOriginalData != null ? mOriginalData : mDataset;
+    }
+
     List<MediaWrapper> getSelection() {
         final List<MediaWrapper> selection = new LinkedList<>();
         for (int i = 0; i < mDataset.size(); ++i) {
-- 
2.11.0



More information about the Android mailing list