Skip to content

Commit

Permalink
Save state for PreferenceSearchFragment
Browse files Browse the repository at this point in the history
  • Loading branch information
italankin committed Jun 5, 2022
1 parent f61c4c2 commit 2366b6d
Showing 1 changed file with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,16 @@ public static PreferenceSearchFragment newInstance(String requestKey) {
return fragment;
}

private static final String STATE_QUERY = "query";

@InjectPresenter
PreferenceSearchPresenter presenter;

private LceLayout lce;
private CompositeAdapter<PreferenceSearchItem> adapter;

private String currentQuery;

@ProvidePresenter
PreferenceSearchPresenter providePresenter() {
return LauncherApp.daggerService.presenters().preferenceSearch();
Expand All @@ -53,6 +57,7 @@ PreferenceSearchPresenter providePresenter() {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
currentQuery = savedInstanceState != null ? savedInstanceState.getString(STATE_QUERY) : null;
}

@Override
Expand Down Expand Up @@ -84,17 +89,18 @@ public void onCreateOptionsMenu(@NonNull Menu menu, @NonNull MenuInflater inflat
MenuItem actionSearchItem = menu.findItem(R.id.action_search);
SearchView searchView = new SearchView(
new ContextThemeWrapper(requireContext(), R.style.AppTheme_Preferences_PreferenceSearch));
searchView.setQuery(currentQuery, false);
actionSearchItem.setActionView(searchView);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
presenter.search(query);
presenter.search(currentQuery = query);
return true;
}

@Override
public boolean onQueryTextChange(String newText) {
presenter.search(newText);
presenter.search(currentQuery = newText);
return true;
}
});
Expand All @@ -115,6 +121,12 @@ public boolean onMenuItemActionCollapse(MenuItem item) {
actionSearchItem.expandActionView();
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
outState.putString(currentQuery, STATE_QUERY);
}

@Override
public void onSearchResults(List<PreferenceSearchItem> items) {
adapter.setDataset(items);
Expand Down

0 comments on commit 2366b6d

Please sign in to comment.