From 3ead365d73d7e1674b3afee8e943249fee4a3b48 Mon Sep 17 00:00:00 2001 From: "David A. Madore" Date: Tue, 27 Apr 2010 18:47:48 +0200 Subject: Retain display (and display history) on configuration change. This works surprisingly well: there must be a grue hiding in the darkness waiting to eat me. :-) --- .../android/unicodeMap/UnicodeMapActivity.java | 38 ++++++++++++++++++---- 1 file changed, 31 insertions(+), 7 deletions(-) (limited to 'src/org') diff --git a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java index 9f8194f..687493e 100644 --- a/src/org/madore/android/unicodeMap/UnicodeMapActivity.java +++ b/src/org/madore/android/unicodeMap/UnicodeMapActivity.java @@ -136,7 +136,7 @@ public final class UnicodeMapActivity extends ListActivity { dispHistory.add(new DisplayAndPosition(curDisp, position, yOffset)); } - protected void doDisplay(Display disp) { + protected void doDisplay(Display disp, boolean isNew) { curDisp = disp; List list; UnicodeArrayAdapter adapter; @@ -168,7 +168,7 @@ public final class UnicodeMapActivity extends ListActivity { list.remove(list.size()-1); adapter = new UnicodeArrayAdapter(this, list); setListAdapter(adapter); - if ( overflowed ) { + if ( overflowed && isNew ) { String str = getResources().getString(R.string.list_too_long); Toast.makeText(UnicodeMapActivity.this, String.format(str, limit), @@ -195,7 +195,7 @@ public final class UnicodeMapActivity extends ListActivity { else if ( it instanceof UnicodeCharacter.Range ) { Display newDisp = new RangeDisplay((UnicodeCharacter.Range)it); saveDisplay(view, position); - doDisplay(newDisp); + doDisplay(newDisp, true); } else throw new AssertionError("unknown UnicodeDisplayable"); } @@ -262,7 +262,19 @@ public final class UnicodeMapActivity extends ListActivity { final boolean needPopulate = db.needPopulate(); if ( needPopulate ) launchPopulation(); - doDisplay(new RootDisplay()); + lv = getListView(); + Object testament = getLastNonConfigurationInstance(); + if ( testament != null ) { + @SuppressWarnings("unchecked") + List oldDispHistory = (List)testament; + dispHistory.clear(); + dispHistory.addAll(oldDispHistory); + assert(dispHistory.size() > 0); + DisplayAndPosition saved = dispHistory.remove(dispHistory.size()-1); + doDisplay(saved.getDisp(), false); + lv.setSelectionFromTop(saved.getSelPosition(), saved.getYOffset()); + } else + doDisplay(new RootDisplay(), true); final ClipboardManager cmgr = (ClipboardManager)getSystemService(Context.CLIPBOARD_SERVICE); final Button btn = (Button) findViewById(R.id.copyButton); @@ -275,20 +287,32 @@ public final class UnicodeMapActivity extends ListActivity { Toast.LENGTH_SHORT).show(); } }); - lv = getListView(); lv.setTextFilterEnabled(true); MapItemClickListener listener = new MapItemClickListener(); lv.setOnItemClickListener(listener); lv.setOnItemLongClickListener(listener); } + @Override + public void onDestroy() { + db.close(); + db = null; + super.onDestroy(); + } + + @Override + public Object onRetainNonConfigurationInstance() { + saveDisplay(); + return dispHistory; + } + final static int searchLimit = 1000; protected void doSearch(String s) { Display newDisp = new SearchDisplay(getResources().getString(R.string.search_results), s, searchLimit); saveDisplay(); - doDisplay(newDisp); + doDisplay(newDisp, true); } protected void querySearch() { @@ -347,7 +371,7 @@ public final class UnicodeMapActivity extends ListActivity { public boolean onKeyDown(int keyCode, KeyEvent event) { if ( ( keyCode == KeyEvent.KEYCODE_BACK ) && dispHistory.size() > 0 ) { DisplayAndPosition saved = dispHistory.remove(dispHistory.size()-1); - doDisplay(saved.getDisp()); + doDisplay(saved.getDisp(), false); lv.setSelectionFromTop(saved.getSelPosition(), saved.getYOffset()); return true; } else if ( keyCode == KeyEvent.KEYCODE_SEARCH ) { -- cgit v1.2.3