title: Can this code avoid the Android handler memory leak?
body: <p><code>handler1</code> is a leak.</p>
  <p>I want to convert <code>handler1</code> code to <code>handler2</code> code. Is that OK?</p>
  <p>What is the difference between the two codes?</p>
  <pre class="lang-java prettyprint-override"><code>public class MainActivity extends AppCompatActivity {
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // leaks!
          Handler handler1 = new Handler()
          {
              @Override
              public void handleMessage(Message msg) {
                  super.handleMessage(msg);
                  Log.e(&quot;LOG&quot;, &quot;Hello~1&quot;);
              }
          };
  
          Handler handler2 = new Handler(new Handler.Callback() {
              @Override
              public boolean handleMessage(Message msg) {
                  Log.e(&quot;LOG&quot;, &quot;Hello~2&quot;);
                  return false;
              }
          });
  
          handler1.postDelayed(new Runnable() {
              @Override
              public void run() { }
          }, 60000);
          handler2.postDelayed(new Runnable() {
              @Override
              public void run() { }
          }, 60000);
  
          finish();
      }
  }
  </code></pre>