title: Why does onUpdate in AppWidgetProvider reset widget state, for 1 second before it returns?
body: <p>I have the following AppWidgetProvider</p>
  
  <pre><code>public class MyWidgetProvider extends AppWidgetProvider {
  
      public final String TAG = "WidgetExample";
  
      @Override
      public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
  
          Log.w(TAG, "onUpdate method called");
          ComponentName thisWidget = new ComponentName(context, MyWidgetProvider.class);
          int[] allWidgetIds = appWidgetManager.getAppWidgetIds(thisWidget);
  
          Intent intent = new Intent("INIT_STATE");
          intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
  
          context.sendBroadcast(intent);
      }
  }
  </code></pre>
  
  <p>I have the following broadcast receiver which is configured to use that action:</p>
  
  <pre><code>public class StateReceiver extends BroadcastReceiver {
  
      public final String TAG = "WidgetExample";
  
      @Override
      public void onReceive(Context context, Intent intent) {
          Log.d(TAG, "StateReceiver got " + intent.getAction());
  
          AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
          RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.widgetlayout);
  
          ComponentName smsComp = new ComponentName(context, SmsReceiver.class);
          PackageManager pm = context.getPackageManager();
  
          if ("INIT_STATE".equals(intent.getAction())) {
              // Lets check state..
              int currentState = pm.getComponentEnabledSetting(smsComp);
              if(currentState == PackageManager.COMPONENT_ENABLED_STATE_DEFAULT || currentState == PackageManager.COMPONENT_ENABLED_STATE_DISABLED){
                  Log.d(TAG, "State is " + currentState + ", matching views to be disabled");
                  setViewsDisable(remoteViews);
              } else {
                  Log.d(TAG, "State is " + currentState + ", matching views to be enabled");
                  setViewsEnable(remoteViews);
              }
          } else if ("TOGGLE_STATE".equals(intent.getAction())) {
              int currentState = pm.getComponentEnabledSetting(smsComp);
              Log.d(TAG, "Current state is " + currentState);
              if (currentState == PackageManager.COMPONENT_ENABLED_STATE_ENABLED) {
                  disable(remoteViews, smsComp, pm);
              } else {
                  enable(remoteViews, smsComp, pm);
              }
          }
  
          int[] allWidgetIds = intent.getIntArrayExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS);
  
          for (int widgetId : allWidgetIds) {
              Log.d(TAG, "Setting the pending intents");
              Intent clickIntent = new Intent("TOGGLE_STATE");
              clickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_IDS, allWidgetIds);
  
              PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, clickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
              remoteViews.setOnClickPendingIntent(R.id.backgroundImage, pendingIntent);
              appWidgetManager.updateAppWidget(widgetId, remoteViews);
          }
      }
  
      private void setViewsEnable(RemoteViews remoteViews) {
          remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.enabledshape);
          remoteViews.setTextViewText(R.id.txtStatus, "ENABLED");
      }
  
      private void setViewsDisable(RemoteViews remoteViews) {
          remoteViews.setImageViewResource(R.id.backgroundImage, R.drawable.disabledshape);
          remoteViews.setTextViewText(R.id.txtStatus, "DISABLED");
      }
  
      private void enable(RemoteViews remoteViews, ComponentName smsComp, PackageManager pm) {
          Log.d(TAG, "enabling");
           setViewsEnable(remoteViews);
          pm.setComponentEnabledSetting(smsComp, PackageManager.COMPONENT_ENABLED_STATE_ENABLED, PackageManager.DONT_KILL_APP);
      }
  
      private void disable(RemoteViews remoteViews, ComponentName smsComp, PackageManager pm) {
          Log.d(TAG, "disabling");
          setViewsDisable(remoteViews);
          pm.setComponentEnabledSetting(smsComp, PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
      }
  }
  </code></pre>
  
  <p><strong>What I want it to do :</strong>
  Simple widget that displays a textview with either ENABLED or DISABLED
  Clicking the widget should switch to the other state
  Upon switching state, it needs to either enable or disable a receiver programatically</p>
  
  <p><strong>What currently happens :</strong>
  It appears that the onUpdate in the AppWidgetProvider is called periodically (by the android system I presume), when this happens, it seems to redraw the widget as it was upon creation, with total disregard to what has been drawn on the remote views since the widget was created.</p>
  
  <p>Are the remove views updated asynchronously? How can I prevent the widget from reverting to its initial state? It's only an issue when I switch to enabled, after the switch it flips back to disabled for about 1 second.</p>