Memory leakage caused by Handler (2) handler Leakage
1. Handler usage upgrade: Use Weak references-solve static internal class access to external class
2. In handlerMessage, set the value for TextView,Pay attention to the weak reference usage in the red box.
Create a static internal Handler class and use weak references to the external objects held by Handler. In this way, the Handler's objects can be recycled, solved the problem of Memory leakage and access to external objects.
However, this is not perfect: After we exit MainActivity, there may still be messages to be processed in the message queue of The logoff thread. What do you mean? After MainActivity exits, there are still messages in the message queue. In our example, after 20 seconds, we also receive messages in the message queue.
Perfect practice: We should remove messages from the message queue when the Activity is closed.