??????Android?????????У??????????????????????й??????????????????????????????????????(?????????)?????????????Handler???????????????? Handler ????????????????
????1?????????
public class LeakActivityDemo extends Activity{
private static TextView mTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextView = new TextView(this);
mTextView.setText("demo");
}
}
??????????????????????????????????д???????????? Activity ?????????mTextView ?????? this ?????
???????????? Activity ???????
???????:?????? Activity ???????? onDestroy ??? mTextView ??????????????????????????
???????:??д??????????????????????????е?? Activity ?????????????????й?.
????2???????????
???????? Handler
public class LeakActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
}
}?? 1);
}
//?????This Handler class should be static or leaks might occur (com.mvp.view.LeakActivity.1)
private Handler mHandler = new Handler() {
@Override public void handleMessage(Message msg) {
super.handleMessage(msg);
}
};
}
?????? Android ?????У??????????? Handler ???????????????????????????????????????????????????????й???
???????????????????????棬??????? static ????????
???????
????1???????? java ?У????????????????????????????????????????????????????????????
????2???? Handler ?????й????????? Handler ?? Looper ??????????????? Activity ??????????????????????
????????????????? Looper?????????????е???????? Hanlder ??????????????????????????? Looper ??????????У?
????????????????????? Handler ??????????????й??????
???????:
????1????t???????????????????????? Activiy ?????? WeakReference(??????)?????????????п??
????2??????Hanlder??????????? ondestroy ???????? Handler ????? Messag ?? Runnable??
???????磺removeCallbacks(Runnable r)??removeMessages(int what)??mHandler.removeCallbacksAndMessages(null);
private final MyHandler mHandler = new MyHandler(this);
public static class MyHandler extends Handler {
private final WeakReference<LeakActivity> mWeakActivity;
public MyHandler(LeakActivity context) {
mWeakActivity = new WeakReference<LeakActivity>(context);
}
@Override public void handleMessage(Message msg) {
super.handleMessage(msg);
if (mWeakActivity.get() != null) {
mWeakActivity.get().todo();
}
}
}
public void todo(){
//todo
}
???????? Runnable
?????????????д?????????????й????????????????????????????????? run ?????????? UI ??????? ????? postDelayed ??????????????????й?????
????mHandler.post(new Runnable() {
????@Override
????public void run() {
????..
????}
????});
???????????????t????Runnable ?? WeakReference ???????????????? WeakReference ?????ж???????????????????
private static final class ItemRunnble implements Runnable {
private final WeakReference<Item> mWeakReference;
public ProgressBarRunnble(Item f) {
mWeakReference = new WeakReference<Item>(f);
}
@Override
public void run() {
Item item = mWeakReference.get();
if (item != null) {
...
}
}
}
mHandler.post(new ItemRunnble(mItem))
????3??????????????????????
????private static MyClass myClass;
????private class MyClass {
????}
????..
????myClass = new MyClass();
???????????????????壬??????????????????????????????????????? Activity ????????? Activity ????????
?????????????????????????
???????
?????????????????????й?????Щ????????????????????ó?????£????е???? Object ??????????
????????????????? remove ???? Object??????????????Щ Ojbect??????????????
?????????????????÷????Android ?б????????? Listener??Observer ???
??????????????????????????????????????????????????????????????????????????????????????????????????????
???????????????????й??????????????????????
?????????о?????????
????· ???????????????????????????????????
????· Bitmip ?? Drawable ??????????
????· ???????????????
????· ????徲? Handler???? Handler ????
????· ??? Application Context??????? Activity Context
????· ????????????????????
????· ??????????y?????????????????????????