I have a Fragment that contains a SearchView. This fragment is include in my HomeActivity and in my SearchResultsActivity.
When the user uses the SearchView in HomeActivity to perform a search, they are shown the results in SearchResultsActivity.
I am trying to make it so the when the user enters a query into the SearchView on HomeActivity, their query String is remembered by the SearchView on the SearchResultsActivity.
I have tried all of the suggestions for this similar issue, but (along with a lot of other developers) the only solution that seems to work does not seem an adequate one - that is, saving the query in a static String in the Fragment class.
Here is my code which shows this approach...
`public class ProductSearchViewFragment extends Fragment {
private static final String LOG_TAG = ProductSearchViewFragment.class.getSimpleName();
private static final String KEY_QUERY = "query";
private static String mQuery = null;
public ProductSearchViewFragment() {
setArguments(new Bundle());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_product_search_view, container, false);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/*
* Populate the searchView if there is a previous query
*/
SearchView searchView = (SearchView)getView().findViewById(R.id.searchView);
if (mQuery != null) {
Log.d(LOG_TAG, "######################### Setting searchView query from static String: " + mQuery);
searchView.setQuery(mQuery, false);
}
else {
Bundle mySavedInstanceState = getArguments();
String query = mySavedInstanceState.getString(KEY_QUERY);
if (query != null) {
// THIS CONDITION IS NEVER MET. I.E., query IS ALWAYS NULL
Log.d(LOG_TAG, "######################### Setting searchView query from bundle arguments: " + query);
searchView.setQuery(query, false);
}
else {
Log.d(LOG_TAG, "######################### Not setting searchView query.");
}
}
}
/*
* Unfortunately, onSaveInstanceState(Bundle outState) is not called,
* so attempt to save the query in our own arguments bundle.
*
*/
@Override
public void onPause() {
super.onPause();
SearchView searchView = (SearchView)getView().findViewById(R.id.searchView);
// Save to static field
mQuery = searchView.getQuery().toString();
// Save to arguments bundle
getArguments().putString(KEY_QUERY, mQuery);
Log.d(LOG_TAG, "#################### saved query: " + mQuery);
}
}`
...I think there must be a better way though than using a static String like this. Please, what is it?
As you'll see from the code, I have tried saving the query in setArguments()
but it is never retrieved.
I've spent some time looking through the Android docs too, which suggests using onSaveInstanceState()
, but that is not called unless the HomeActivity is destroyed (which does not happen and neither do I want to force it).