Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>One solution to this problem is to use the <a href="http://developer.android.com/reference/android/os/Handler.html" rel="noreferrer">Handler.postDelayed()</a> method. Some Google <a href="https://cswithandroid.withgoogle.com/lesson-plans/facilitator-guide.html" rel="noreferrer">training materials</a> suggest the same solution.</p> <pre><code>@Override public void onClick(View v) { my_button.setBackgroundResource(R.drawable.icon); Handler handler = new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { my_button.setBackgroundResource(R.drawable.defaultcard); } }, 2000); } </code></pre> <p>However, some have pointed out that the solution above <a href="http://www.androiddesignpatterns.com/2013/01/inner-class-handler-memory-leak.html" rel="noreferrer">causes a memory leak</a> because it uses a non-static inner and anonymous class which implicitly holds a reference to its outer class, the activity. This is a problem when the activity context is garbage collected. </p> <p>A more complex solution that avoids the memory leak subclasses the <code>Handler</code> and <code>Runnable</code> with static inner classes inside the activity since static inner classes do not hold an implicit reference to their outer class:</p> <pre><code>private static class MyHandler extends Handler {} private final MyHandler mHandler = new MyHandler(); public static class MyRunnable implements Runnable { private final WeakReference&lt;Activity&gt; mActivity; public MyRunnable(Activity activity) { mActivity = new WeakReference&lt;&gt;(activity); } @Override public void run() { Activity activity = mActivity.get(); if (activity != null) { Button btn = (Button) activity.findViewById(R.id.button); btn.setBackgroundResource(R.drawable.defaultcard); } } } private MyRunnable mRunnable = new MyRunnable(this); public void onClick(View view) { my_button.setBackgroundResource(R.drawable.icon); // Execute the Runnable in 2 seconds mHandler.postDelayed(mRunnable, 2000); } </code></pre> <p>Note that the <code>Runnable</code> uses a <a href="https://developer.android.com/reference/java/lang/ref/WeakReference.html" rel="noreferrer">WeakReference</a> to the Activity, which is necessary in a static class that needs access to the UI.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload