diff options
author | Bananeweizen <bananeweizen@gmx.de> | 2012-12-03 17:50:17 +0100 |
---|---|---|
committer | Bananeweizen <bananeweizen@gmx.de> | 2012-12-03 17:50:17 +0100 |
commit | ff52a912f6e8d82fbd626b0ae9fa6af302f659d1 (patch) | |
tree | 616ee5545d423adccc460d3d13732aae65424174 /main/src/cgeo/geocaching | |
parent | d9887e50d58d089c9379faca4f3b4d0e3bf5d564 (diff) | |
download | cgeo-ff52a912f6e8d82fbd626b0ae9fa6af302f659d1.zip cgeo-ff52a912f6e8d82fbd626b0ae9fa6af302f659d1.tar.gz cgeo-ff52a912f6e8d82fbd626b0ae9fa6af302f659d1.tar.bz2 |
refactoring: introduce handler with weak reference to activity
* remaining handler classes need to be changed also if no issues occur
Diffstat (limited to 'main/src/cgeo/geocaching')
-rw-r--r-- | main/src/cgeo/geocaching/cgeocaches.java | 119 | ||||
-rw-r--r-- | main/src/cgeo/geocaching/ui/WeakReferenceHandler.java | 27 |
2 files changed, 94 insertions, 52 deletions
diff --git a/main/src/cgeo/geocaching/cgeocaches.java b/main/src/cgeo/geocaching/cgeocaches.java index 45eb9af..74793c0 100644 --- a/main/src/cgeo/geocaching/cgeocaches.java +++ b/main/src/cgeo/geocaching/cgeocaches.java @@ -28,6 +28,7 @@ import cgeo.geocaching.sorting.EventDateComparator; import cgeo.geocaching.sorting.VisitComparator; import cgeo.geocaching.ui.CacheListAdapter; import cgeo.geocaching.ui.LoggingUI; +import cgeo.geocaching.ui.WeakReferenceHandler; import cgeo.geocaching.utils.GeoDirHandler; import cgeo.geocaching.utils.Log; import cgeo.geocaching.utils.RunnableWithArgument; @@ -156,57 +157,42 @@ public class cgeocaches extends AbstractListActivity { */ private MenuItem navigationMenu; - private Handler loadCachesHandler = new Handler() { - - @Override - public void handleMessage(Message msg) { - try { - setAdapter(); - - updateTitle(); - - setDateComparatorForEventList(); + public void handleCachesLoaded() { + try { + setAdapter(); - showFooterMoreCaches(); + updateTitle(); - if (search != null && search.getError() == StatusCode.UNAPPROVED_LICENSE) { - AlertDialog.Builder dialog = new AlertDialog.Builder(cgeocaches.this); - dialog.setTitle(res.getString(R.string.license)); - dialog.setMessage(res.getString(R.string.err_license)); - dialog.setCancelable(true); - dialog.setNegativeButton(res.getString(R.string.license_dismiss), new DialogInterface.OnClickListener() { - - @Override - public void onClick(DialogInterface dialog, int id) { - Cookies.clearCookies(); - dialog.cancel(); - } - }); - dialog.setPositiveButton(res.getString(R.string.license_show), new DialogInterface.OnClickListener() { + setDateComparatorForEventList(); - @Override - public void onClick(DialogInterface dialog, int id) { - Cookies.clearCookies(); - startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/software/agreement.aspx?ID=0"))); - } - }); + showFooterMoreCaches(); - AlertDialog alert = dialog.create(); - alert.show(); - } else if (search != null && search.getError() != null) { - showToast(res.getString(R.string.err_download_fail) + ' ' + search.getError().getErrorString(res) + '.'); + if (search != null && search.getError() == StatusCode.UNAPPROVED_LICENSE) { + AlertDialog.Builder dialog = new AlertDialog.Builder(cgeocaches.this); + dialog.setTitle(res.getString(R.string.license)); + dialog.setMessage(res.getString(R.string.err_license)); + dialog.setCancelable(true); + dialog.setNegativeButton(res.getString(R.string.license_dismiss), new DialogInterface.OnClickListener() { - hideLoading(); - showProgress(false); + @Override + public void onClick(DialogInterface dialog, int id) { + Cookies.clearCookies(); + dialog.cancel(); + } + }); + dialog.setPositiveButton(res.getString(R.string.license_show), new DialogInterface.OnClickListener() { - finish(); - return; - } + @Override + public void onClick(DialogInterface dialog, int id) { + Cookies.clearCookies(); + startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.geocaching.com/software/agreement.aspx?ID=0"))); + } + }); - setAdapterCurrentCoordinates(false); - } catch (Exception e) { - showToast(res.getString(R.string.err_detail_cache_find_any)); - Log.e("cgeocaches.loadCachesHandler: " + e.toString()); + AlertDialog alert = dialog.create(); + alert.show(); + } else if (search != null && search.getError() != null) { + showToast(res.getString(R.string.err_download_fail) + ' ' + search.getError().getErrorString(res) + '.'); hideLoading(); showProgress(false); @@ -215,16 +201,45 @@ public class cgeocaches extends AbstractListActivity { return; } - try { - hideLoading(); - showProgress(false); - } catch (Exception e2) { - Log.e("cgeocaches.loadCachesHandler.2: " + e2.toString()); - } + setAdapterCurrentCoordinates(false); + } catch (Exception e) { + showToast(res.getString(R.string.err_detail_cache_find_any)); + Log.e("cgeocaches.loadCachesHandler: " + e.toString()); - if (adapter != null) { - adapter.setSelectMode(false); + hideLoading(); + showProgress(false); + + finish(); + return; + } + + try { + hideLoading(); + showProgress(false); + } catch (Exception e2) { + Log.e("cgeocaches.loadCachesHandler.2: " + e2.toString()); + } + + if (adapter != null) { + adapter.setSelectMode(false); + } + } + + private Handler loadCachesHandler = new LoadCachesHandler(this); + + private static class LoadCachesHandler extends WeakReferenceHandler<cgeocaches> { + + protected LoadCachesHandler(cgeocaches activity) { + super(activity); + } + + @Override + public void handleMessage(Message msg) { + final cgeocaches activity = getActivity(); + if (activity == null) { + return; } + activity.handleCachesLoaded(); } }; private Handler loadNextPageHandler = new Handler() { diff --git a/main/src/cgeo/geocaching/ui/WeakReferenceHandler.java b/main/src/cgeo/geocaching/ui/WeakReferenceHandler.java new file mode 100644 index 0000000..4724466 --- /dev/null +++ b/main/src/cgeo/geocaching/ui/WeakReferenceHandler.java @@ -0,0 +1,27 @@ +package cgeo.geocaching.ui; + +import android.app.Activity; +import android.os.Handler; + +import java.lang.ref.WeakReference; + +/** + * Standard handler implementation which uses a weak reference to its activity. This avoids that activities stay in + * memory due to references from the handler to the activity (see Android Lint warning "HandlerLeak") + * + * Create static private subclasses of this handler class in your activity. + * + * @param <ActivityType> + */ +public abstract class WeakReferenceHandler<ActivityType extends Activity> extends Handler { + + private final WeakReference<ActivityType> activityRef; + + protected WeakReferenceHandler(final ActivityType activity) { + this.activityRef = new WeakReference<ActivityType>(activity); + } + + protected ActivityType getActivity() { + return activityRef.get(); + } +} |