X-Git-Url: http://gitweb.fperrin.net/?p=Dictionary.git;a=blobdiff_plain;f=src%2Fcom%2Fhughes%2Fandroid%2Fdictionary%2FDictionaryActivity.java;h=9a4e913b69927d986b1bfd2543d8a637d49de10e;hp=4e1bf6572d453b3d028c62778b10db5c33ab1474;hb=8a4bb75be2e165be16d9d8e345d0dca5aca169ad;hpb=81e7773623371b4fc2de7bee9a2ad8112dddb30a diff --git a/src/com/hughes/android/dictionary/DictionaryActivity.java b/src/com/hughes/android/dictionary/DictionaryActivity.java index 4e1bf65..9a4e913 100644 --- a/src/com/hughes/android/dictionary/DictionaryActivity.java +++ b/src/com/hughes/android/dictionary/DictionaryActivity.java @@ -222,6 +222,7 @@ public class DictionaryActivity extends AppCompatActivity { 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) { @@ -254,6 +255,16 @@ public class DictionaryActivity extends AppCompatActivity { 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); @@ -625,8 +636,22 @@ public class DictionaryActivity extends AppCompatActivity { t.setText(c.getString(1)); } }); + // Set up search history - addToSearchHistory(text); + ArrayList 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); @@ -754,6 +779,12 @@ public class DictionaryActivity extends AppCompatActivity { prefs.commit(); } + @Override + protected void onPause() { + super.onPause(); + saveSearchHistory(); + } + @Override protected void onDestroy() { super.onDestroy(); @@ -1395,6 +1426,7 @@ public class DictionaryActivity extends AppCompatActivity { if (hideKeyboard) { hideKeyboard(); } + addToSearchHistory(text); } private void setSearchText(final String text, final boolean triggerSearch) {