11

Im having a search view in my action bar and i want to restore the query after orientation changed. How can I do this? Setting the query from the bundle using SearchView.setQuery(query, false); doesn't work as the searchview sets its query to "" as soon as it expands.

Heres the relevant code in my onActivityCreated:

if(savedInstanceState != null && savedInstanceState.containsKey(BUNDLE_KEY_SEARCH))
    mCurFilter = savedInstanceState.getString(BUNDLE_KEY_SEARCH);

And here my onSaveInstanceState:

savedInstanceState.putString(BUNDLE_KEY_SEARCH, mCurFilter);
super.onSaveInstanceState(savedInstanceState);

So how can I restore its state as im not able to do it using setQuery? Thanks to all

tobs
  • 699
  • 2
  • 8
  • 24

4 Answers4

3

Yes, it is possible to restore the state of search view widget:

Use below code to save the search query text:

private String mSearchQuery;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater menuInflater = getMenuInflater();

    menuInflater.inflate(R.menu.search_menu, menu);

    // Associate searchable configuration with the SearchView
    SearchManager searchManager =
            (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView =
            (SearchView) menu.findItem(R.id.search).getActionView();

  searchView.setSearchableInfo(
            searchManager.getSearchableInfo(getComponentName()));

    if(mSearchQuery != null){
        searchView.setIconified(true);
        searchView.onActionViewExpanded();
        searchView.setQuery(mSearchQuery, false);
        searchView.setFocusable(true);
    }

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener(){

        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            mSearchQuery = newText;
            return false;
        }
    });

    return true;
}

 @Override
protected void onSaveInstanceState(Bundle outState) {
    outState.putString("searchQuery", mSearchQuery);
    super.onSaveInstanceState(outState);
}

Then in OnCreate() method of activity, add following lines:

    if(savedInstanceState != null){
        mSearchQuery = savedInstanceState.getString("searchQuery");
    }
Sagar Rathod
  • 542
  • 8
  • 13
2

As a workaround, replace your

mSearchView.setQuery(mSearchText, false); 

with:

final String s = mSearchText;

mSearchView.post(new Runnable() {

    @Override
    public void run() {
        mSearchView.setQuery(s, false);
    }
});

It will set the saved string after the system has set the empty string.

Frank
  • 12,010
  • 8
  • 61
  • 78
0

There is better (in my opinion) way (without posting Runnable) to avoid setting query to "" when you expand yours SearchView.

1) You will need one more String variable to store query.

//String that you fill in onQueryTextChange
String searchText;
//String, that is filled after you restore previous
String queryToSave;

2) After getting yours query from Bundle, store it in another variable.

if(this.searchText!=null)
{
    this.queryToSave=this.searchText;
}

3) In onQueryTextChange(String query) you fill searchText variable to text or to null if it's empty

4) In onMenuItemActionExpand(MenuItem item) you need save yours restored String (searchText) to another variable (queryToSave), because it (searchText) will set to null after there will be call to onQueryTextChange with empty text

if(searchText!=null)
{
    queryToSave=new StringBuffer(searchText).toString();
}

5) So now if you call

mSearchView.setQuery(queryToSave, false);

after expanding SearchView queryToSave won't be empty and you'll see expanded SearchView with yours restored text!

mohax
  • 4,435
  • 2
  • 38
  • 85
0

you should make this:

import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.SearchView;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity {

    private SearchView mSearchView;
    private String mSearchString;
    private static final String SEARCH_KEY = "search";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // if you saved something on outState you can recover them here
        if (savedInstanceState != null) {
            mSearchString = savedInstanceState.getString(SEARCH_KEY);
        }
    }

    // This is called before the activity is destroyed
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mSearchString = mSearchView.getQuery().toString();
        outState.putString(SEARCH_KEY, mSearchString);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);

        MenuItem searchMenuItem = menu.findItem(R.id.menu_main_action_search);

        mSearchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);

        //focus the SearchView
        if (mSearchString != null && !mSearchString.isEmpty()) {
            searchMenuItem.expandActionView();
            mSearchView.setQuery(mSearchString, true);
            mSearchView.clearFocus();
        }

        return super.onCreateOptionsMenu(menu);
    }
}
Cabezas
  • 9,329
  • 7
  • 67
  • 69