The question now is what I wrote in the class handler, always prompts a warning:
This Handler class should be static or leaks might occur
I searched the Internet this warning, the resulting solution is
Handler class should be static type, or they may cause leakage. In the process queued messages in the message queue maintained its target Handler class applications. If Handler is an inner class, then it will remain it in the outer class references. In order to avoid the disclosure of this external class Handler should be declared as static nested class, and use the weak application of the outer class.
about usage examples are:
static class MyHandler extends Handler {
WeakReference<PopupActivity> mActivity;
MyHandler(PopupActivity activity) {
mActivity = new WeakReference<PopupActivity>(activity);
}
@Override
public void handleMessage(Message msg) {
PopupActivity theActivity = mActivity.get();
switch (msg.what) {
case 0:
theActivity.popPlay.setChecked(true);
break;
}
}
};
MyHandler ttsHandler = new MyHandler(this);
private Cursor mCursor;
private void test() {
ttsHandler.sendEmptyMessage(0);
}
assume that the handler class is B, this way, handler defined as static, in order to use the handler class defined in class A, a number of variables, these variables in class A have to be defined as static.
I would like to ask about, so a lot of static variables affect the operation of the program do? It will not be a waste of memory?
Thank you ~
------ Solution --------------------------------- -----------
Haha, it should be no problem. This is not a lot of static. .
------ Solution ---------------------------------------- ----
your thinking a little a little bit wrong
static class is to disconnect the link MyHandler and outside class, so that the inner class and outside class does not have any contact is static class purposes.
You do not need to declare a large number of the Activity by static variables to provide to the handler to use (this code is not only unsightly but the design is very unreasonable). Because you have a mActivity MyHandler weak reference so you can call mActivity any public functions and variables.
So, you just need to be defined in mAcivity public XXX getXXX () method to provide MyHandler you need a variable package together, we can be friends.
------ Solution ---------------------------------------- ----
If the handler is not static, then when after this activity destory may not be garbage collected, thus causing the so-called memory leak
------ reference ----- ----------------------------------
dozen variables did not affect me?
------ reference --------------------------------------- < br>
ah, really a awoken ah, thank you to answer, I understand ^ _ ^
------ reference -------------- -------------------------
hello, and then ask you a question, I used mActivity direct access to which it belongs Activity The variables (including private), can be accessed. No use setter and getter (found using getter and setter, then a lot of variables to be set getter and setter), I do not know my way in the design of this access whether it is reasonable?
Also please advise, thank ~
------ reference ----------------------------- ----------
inner classes, I can.
------ reference --------------------------------------- < br>
Oh, thank ~
------ reference ----------------------------- ----------
this learning a teachable
没有评论:
发表评论