This Handler class should be static or leaks may occur (null)

13.3k views Asked by At
This Handler class should be static or leaks may occur (null)

Is the 'class' this message referring to 'MyActivity' here, since Handler is an object and i did declare it static. Should I ignore it or there something I should add, like 'static' somewhere in the 'MyActivity' declaration (I tried this and got errors). I notice 'WeakReference' is often suggested for this lint warning.

public class MyActivity extends Activity{
...
static Handler handler;
...
handler = new Handler()
  {
    public void handleMessage(Message msg) {
2

There are 2 answers

6
CommonsWare On BEST ANSWER

since Handler is an object and i did declare it static

You declared the data member to be static. However, you are using anonymous inner class, and therefore your subclass of Handler is not static.

Instead of:

  handler = new Handler() {
    public void handleMessage(Message msg) {
      // do cool stuff
    }
  };

use:

handler=new MyVeryOwnHandler();

where MyVeryOwnHandler is either a regular Java class or a static inner class:

private static class MyVeryOwnHandler extends Handler {
  public void handleMessage(Message msg) {
      // do cool stuff
  }
};

Note that the error is that the class needs to be static; it does not say that the object needs to be static.

0
droid192 On

To avoid leaks I also migrated to a static nested class. The explanation from Android Studio says this, it might help:

Since this Handler is declared as an inner class, it may prevent the outer class from being garbage collected. If the Handler is using a Looper or MessageQueue for a thread other than the main thread, then there is no issue. If the Handler is using the Looper or MessageQueue of the main thread, you need to fix your Handler declaration, as follows: Declare the Handler as a static class; In the outer class, instantiate a WeakReference to the outer class and pass this object to your Handler when you instantiate the Handler; Make all references to members of the outer class using the WeakReference object.

Before migration I used the implementation of a Looper thread as suggested by developer.android.com , which in the end lead to the warning. *scratch

  class LooperThread extends Thread {
  public Handler mHandler;

  public void run() {
      Looper.prepare();

      mHandler = new Handler() {
          public void handleMessage(Message msg) {
              // process incoming messages here
          }
      };

      Looper.loop();
  }  
 }