2

I'm working on some Activity code and ran across an Android Studio warning I don't understand. Here is a minimal version of the code that produces the warning:

package com.example.test;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;

public class TestActivity extends Activity {

    private static Handler mActivityHandler = new Handler() {
        // Android Studio 1.5.1 warns of a leak here
    };

    protected void onCreate( Bundle state ) {
        super.onCreate( state );
        mActivityHandler.sendEmptyMessageDelayed( 0, 10L );
    }
}

Android Studio 1.5.1 highlights in yellow the body of the new Handler() { } where the comment indicates, displaying this message:

Handler reference leaks

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.

The code runs fine (when the rest of the code that I stripped out for this example is put back in!) but I would like to understand the warning and find a way to fix it.

Is there another way to write the code to avoid this warning?

Michael Geary
  • 28,450
  • 9
  • 65
  • 75
  • 2
    Both a good explanation and fix for the warning can be found [here](http://stackoverflow.com/questions/11407943/this-handler-class-should-be-static-or-leaks-might-occur-incominghandler). – George Mulligan Apr 20 '16 at 03:43
  • 2
    Your particular example is interesting to me since you made a static instance of the anonymous `Handler`. I'm curious if the warning really applies in this case or not since you don't have the implicit reference to `TestActivity.this`. – George Mulligan Apr 20 '16 at 03:54
  • How is the explanation and suggested remedy not sufficient? – 323go Apr 20 '16 at 04:07
  • @323go: I'm sure the explanation is sufficient for an experienced Android and Java developer. But if I'm puzzled by this, I suspect other people may be too. For the moment, I'm happy enough to just run with the code as is, even with the warning. But of course I don't like warnings like this! So I posted the question in case it helps anyone else in my shoes. :-) – Michael Geary Apr 20 '16 at 04:21

1 Answers1

1

I also got this warning today. Although I am not a experienced developer like you but, here, you can declare mActivityHandler as a global object and implement Handler.Callback, and then, instantiate mActivityHandler by passing context of the activity.

Is there another way to write the code to avoid this warning?

    public class TestActivity extends Activity implements Handler.Callback {

    private static Handler mActivityHandler;

    protected void onCreate( Bundle state ) {
        super.onCreate( state );
        mActivityHandler=new Handler(this);
        mActivityHandler.sendEmptyMessageDelayed( 0, 10L );
    }

    @Override
    public boolean handleMessage(@NonNull Message msg) {
        return true;
    }
}

This helped me in removing the warning of Handlerleak.

Mrudul Tora
  • 715
  • 8
  • 14