Retain searchView state when changing orientation in fragment

I am trying to use actionbarcompat (with android support library) following the android developers blog .

Basically, I am trying to create a fragment extending ListFragment. I am using ArrayAdapter for listview. I was able to successfully integrate compatibility with the search file into the search menu and get the ability to search.

Now I want to save the state of the fragment when the orientation changes. I did not set setRetainInstance (true) in my fragment. To save the state of the search view, I tried the following:

  • save text in SearchView in onSaveInstanceState ()
  • in onCreateView, retrieve searchText if available
  • in onCreateOptionsMenu (which will be called each time the orientation changes), I try to set the search query in the SearchView instance mSearchView.setQuery(mSearchText, false);

There are two questions that I see with this approach:

  • onQueryTextChange () is called twice when the orientation changes - once with the saved search tag (due to mSearchView.setQuery(mSearchText, false); ) and again with an empty String value. This second call with an empty String value updates the list adapter to have all the items without any filtering. I am also not quite sure why this is happening.
  • mSearchView.setQuery(mSearchText, false); it doesn’t set the query in SearchView and is not displayed in the user interface (when changing orientation, the default view expands by default and focuses without any text value, although I specified the query).

The structure of my fragment is as follows:

 @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // create and initialize list adapter .... setHasOptionsMenu(true); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.plainlist, container, false); if (savedInstanceState != null) { mSearchText = savedInstanceState.getString(RetainDataKeySearchText, null); } return view; } @Override public void onSaveInstanceState(Bundle outState) { if (isAdded()) { if (mSearchView != null) { String searchText = mSearchView.getQuery().toString(); if (!TextUtils.isEmpty(searchText)) outState.putString(RetainDataKeySearchText, searchText); } } super.onSaveInstanceState(outState); } @Override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { inflater.inflate(R.menu.search_item_list, menu); MenuItem searchItem = menu.findItem(R.id.menu_search); mSearchView = (SearchView) MenuItemCompat.getActionView(searchItem); mSearchView.setOnQueryTextListener(this); if (!TextUtils.isEmpty(mSearchText)) mSearchView.setQuery(mSearchText, false); super.onCreateOptionsMenu(menu, inflater); } @Override public boolean onQueryTextChange(String newText) { // Called when the action bar search text has changed. searchItems(newText); return true; } @Override public boolean onQueryTextSubmit(String searchText) { return true; } private void searchItems(String searchText) { // filter results and update the list adapter } 

The xml file (search_item_list) of the menu is as follows:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:sprinklr="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/menu_search" android:icon="@android:drawable/ic_menu_search" android:title="@string/search" android:orderInCategory="0" sprinklr:showAsAction="ifRoom|collapseActionView" sprinklr:actionViewClass="android.support.v7.widget.SearchView" /> </menu> 

I would like to know if there is something that I am missing, or if there is a better alternative to keep the state of the SearchView ( android.support.v7.widget.SearchView ) when changing the orientation in the ListFragment using actionbarcompat.

+4
source share
1 answer

Replace as a workaround

 mSearchView.setQuery(mSearchText, false); 

from:

 final String s = mSearchText; mSearchView.post(new Runnable() { @Override public void run() { mSearchView.setQuery(s, false); } }); 

It will set the saved line after the system sets the empty line.

+2
source

Source: https://habr.com/ru/post/1499408/


All Articles