0


when I implement the handler I get warning

' This Handler class should be static or leaks might occur '
Because above warning I defined the handler as a static class. below is after modify.

public static Handler   _handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {


        Bundle bundle = msg.getData();
        .
        .           
        .
        super.handleMessage(msg);
    }

};

Now my this Handler will receive messages from many threads. So I want to know is it OK to define this handler as a static? Handler keep the all the receiving messages in a queue and process one by one without missing anything ?

user2771655
  • 1,052
  • 3
  • 20
  • 38
  • possible duplicate of [This Handler class should be static or leaks might occur: IncomingHandler](http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler) – Maxim Efimov Sep 18 '13 at 03:00
  • That way did not work for me. My question is not to resolve the lint error. But about the synchronization. If I defined the class as static do I get all the messages from many threads without missing anything. – user2771655 Sep 18 '13 at 03:06
  • I think it's fine in terms of syncronization. – Maxim Efimov Sep 18 '13 at 03:13
  • If we can simply use this why do we need to use this code? - http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler – user2771655 Sep 18 '13 at 03:38
  • The main point is that Handler class(not instance) should be static as it unbind it from parent class because without that Handler can't exist without being wrapped by parent class. In your code anonymous class is fine with that. – Maxim Efimov Sep 18 '13 at 03:54
  • possible duplicate of [This Handler class should be static or leaks might occur (com.test.test3.ui.MainActivity.1)](http://stackoverflow.com/questions/17899328/this-handler-class-should-be-static-or-leaks-might-occur-com-test-test3-ui-main) – Raghunandan Sep 18 '13 at 04:07
  • Hi Maxim, Can you please tell me how can I access the out class method from my handleMessage(Message msg) {} method? because based on the message I receive I need to do some work. So I would like to handover that work to a method in outer class. – user2771655 Sep 18 '13 at 05:19
  • @Raghunandan, If I used the way you propose I am unable to send the message to specific handler. Because I'm sending the message from a thread which doesn't know about the Activity instance. Do you know a way to find the handler defined in Activity from a running thread? who does not know each other. – user2771655 Sep 18 '13 at 06:47
  • To access the outer class, use a WeakReference object to reference it, and initialize it in the handler constructor. – Sogger Jan 07 '15 at 18:37

1 Answers1

0

Handlers always process messages one at a time in the order they are posted/scheduled for regardless of if the class is static or not.

If it is not static, the worry is you can leak the class reference for a long period of time: This Handler class should be static or leaks might occur: IncomingHandler.

Community
  • 1
  • 1
Sogger
  • 15,962
  • 6
  • 43
  • 40