title: Runnable not stopping at an Android Widget
body: <p>I have an android widget which I want to update its own textview every 5 seconds.</p>
  
  <p>Think of it as a news widget where the most important news will show on the widget one at a time. For demonstration purposes i will just use an array to show you the different texts.</p>
  
  <p>The code is this: </p>
  
  <pre><code>public class ListingsWatchWidget extends AppWidgetProvider {
  
      private static int WIDGET_REFRESH_TIME = 5;
      public Handler handler = new Handler();
  
      //private RemoteViews views;    
      private int REFRESH_COUNT = 0;
      private String[] titles = {"Title 1", "Title 2", "Title 3"};
  
      public boolean killMe = false;
  
      private Context ctx;
  
      Runnable updateWidgetText = new Runnable()
      {
          @Override
          public void run() {
  
          Log.i("ListingsWatchWidget", "killMe is " + ListingsWatchWidget.this.killMe);
          if (ListingsWatchWidget.this.killMe) { return; }
  
          AppWidgetManager gm = AppWidgetManager.getInstance(ctx);                        
          ComponentName thisWidget = new ComponentName(ctx, ListingsWatchWidget.class);         
  
          int index = (int)(REFRESH_COUNT % titles.length);
          Log.i("ListingsWatchWidget", "index = " + index);
  
          RemoteViews views = new RemoteViews(ctx.getPackageName(),R.layout.widget);    
          views.setTextViewText(R.id.titleTextView, titles[index]);  
  
          gm.updateAppWidget(thisWidget, views);
  
          REFRESH_COUNT++;
  
          handler.postDelayed(this, WIDGET_REFRESH_TIME * 1000);
  
          }   
      };  
  
  
      @Override
      public void onReceive(Context context, Intent intent) {
          super.onReceive(context, intent);
  
          Log.i("ListingsWatchWidget", intent.getAction());
  
          if (intent.getAction().equals("android.appwidget.action.APPWIDGET_ENABLED")) {
              handler.removeCallbacks(updateWidgetText);
              ctx = context;
              handler.postDelayed(updateWidgetText, WIDGET_REFRESH_TIME*1000);
          }
          else if (intent.getAction().equals("android.appwidget.action.APPWIDGET_DISABLED")) {
              handler.removeCallbacks(updateWidgetText);
              this.killMe = true;
          }
  
      }
  
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
          //TODO ...
      }
  
  }
  </code></pre>
  
  <p>Everything works as expected except when the user removes the widget from his desktop. The runnable continues to run endlessly.</p>
  
  <p>I have tried various solutions suggests as <a href="https://stackoverflow.com/questions/5844308/removecallbacks-not-stopping-runnable">this</a> and <a href="https://stackoverflow.com/questions/5885587/how-to-cancel-handler-in-code">this</a> but none worked.</p>
  
  <p>Let me give you a fragment if the logger when adding the widget to the desktop and when removing it:</p>
  
  <pre><code>06-20 22:22:57.318: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_ENABLED
  06-20 22:22:57.328: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_UPDATE
  06-20 22:23:02.323: I/ListingsWatchWidget(4012): killMe is false
  06-20 22:23:02.323: I/ListingsWatchWidget(4012): index = 0
  06-20 22:23:06.857: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_DELETED
  06-20 22:23:06.877: I/ListingsWatchWidget(4012): android.appwidget.action.APPWIDGET_DISABLED
  06-20 22:23:07.338: I/ListingsWatchWidget(4012): killMe is false
  06-20 22:23:07.338: I/ListingsWatchWidget(4012): index = 1
  </code></pre>
  
  <p>After the widget is deleted the runnable should had stop ...</p>