SoftReference可以用於bitmap緩存 WeakReference 可以用於handler ...
WeakReference 可以用於handler
private Handler mRemoteHandler = new SafeHandler(this); private static final int INIT_UI = 1; /** * safe handler */ private static class SafeHandler extends Handler { private final WeakReference<MainActivity> mActivitys; public SafeHandler(MainActivity preview) { mActivitys = new WeakReference<MainActivity>(preview); } @Override public void handleMessage(Message msg) { MainActivity activity = mActivitys.get(); if (activity != null) { if (null != msg) { switch (msg.what) { case INIT_UI: activity.initView(); activity.updateUI(); break; default: break; } } } } }