I have two SearchViews in one xml layout:
One way to alleviate this problem is to capture the orientation event change with your activity and then set the query again on your two search views.
The SearchView
uses as its content the view resulted from inflating a layout file. As a result, all the SearchViews
used in the layout of an activity(like your case) will have as content, views with the same ids. When Android will try to save the state to handle the configuration change it will see that the EditTexts
from the SearchViews
have the same id and it will restore the same state for all of them.
The simplest way to handle this issue is to use the Activity
's onSaveInstanceState
and onRestoreInstanceState
like this:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
// state for the first SearchView
outState.putString("sv1", firstSearchView.getQuery().toString());
// state for the second SearchView
outState.putString("sv2", secondSearchView.getQuery().toString());
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// properly set the state to balance Android's own restore mechanism
firstSearchView.setQuery(savedInstanceState.getString("sv1"), false);
secondSearchView.setQuery(savedInstanceState.getString("sv2"), false);
}
Also have a look at this related question.
Add this to manifest in activity in which you are having two SearchView
android:configChanges="keyboardHidden|orientation|screenSize"