public static final String DICT_FILE = "dictFile";
public static final String INDEX_SHORT_NAME = "indexShortName";
public static final String SEARCH_TOKEN = "searchToken";
+ public static final String SEARCH_HISTORY = "searchHistory";
public static final String CAN_AUTO_LAUNCH_DICT = "canAutoLaunch";
public static final String SHOW_DOWNLOADABLE = "showLocal";
Log.d(LOG, "onSaveInstanceState: " + searchView.getQuery().toString());
outState.putString(C.INDEX_SHORT_NAME, index.shortName);
outState.putString(C.SEARCH_TOKEN, searchView.getQuery().toString());
+ outState.putStringArrayList(C.SEARCH_HISTORY, searchHistory);
}
private int getMatchLen(String search, Index.IndexEntry e) {
finish();
}
+ private void saveSearchHistory() {
+ final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
+ final SharedPreferences.Editor ed = prefs.edit();
+ for (int i = 0; i < searchHistory.size(); i++) {
+ ed.putString("history" + i, searchHistory.get(i));
+ }
+ ed.remove("history" + searchHistory.size());
+ ed.apply();
+ }
+
private void addToSearchHistory(String text) {
if (text == null || text.isEmpty()) return;
int exists = searchHistory.indexOf(text);
t.setText(c.getString(1));
}
});
+
// Set up search history
- addToSearchHistory(text);
+ ArrayList<String> savedHistory = null;
+ if (savedInstanceState != null) savedHistory = savedInstanceState.getStringArrayList(C.SEARCH_HISTORY);
+ if (savedHistory != null && !savedHistory.isEmpty()) {
+ } else {
+ savedHistory = new ArrayList<>();
+ for (int i = 0; i < MAX_SEARCH_HISTORY; i++) {
+ String h = prefs.getString("history" + i, null);
+ if (h == null) break;
+ savedHistory.add(h);
+ }
+ }
+ for (int i = savedHistory.size() - 1; i >= 0; i--) {
+ addToSearchHistory(savedHistory.get(i));
+ }
setSearchText(text, true);
Log.d(LOG, "Trying to restore searchText=" + text);
prefs.commit();
}
+ @Override
+ protected void onPause() {
+ super.onPause();
+ saveSearchHistory();
+ }
+
@Override
protected void onDestroy() {
super.onDestroy();
if (hideKeyboard) {
hideKeyboard();
}
+ addToSearchHistory(text);
}
private void setSearchText(final String text, final boolean triggerSearch) {