title: Android: animation in action bar does not work with async task
body: <p><strong>Goal</strong>: To have a connect icon in the action bar. OnClick, it should try to connect in the background. While it is trying to connect, the connection animation should play. It should then switch back to "not_connected" or "connected" drawable, depending on success or failure. </p>
  
  <p><strong>Problem</strong>: If I call the code inside the onClick method of the Custom Action Provider, it works perfectly (see commented-out portion that toggles it). When the same exact code is inside onPreExecute of the AsyncTask, it will not play (it stays on the first frame), even though I am passing a reference to the ImageView.</p>
  
  <p><strong>My setup</strong>: To do a Frame Animation in the action bar, you have to use a Custom Action Provider (see <a href="https://stackoverflow.com/questions/13940472/animationdrawable-is-not-playing-in-actionbar">animationDrawable is not playing in Actionbar?</a>). So, I have a custom layout, a Custom Action Provider that inflates it and sets up the on-click method. My connection functionality is inside an AsyncTask so that it will connect asynchronously.</p>
  
  <p><strong>Any ideas?</strong></p>
  
  <p>menu.xml </p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;menu xmlns:android="http://schemas.android.com/apk/res/android" &gt;
      &lt;item
          android:id="@+id/connect"
          android:showAsAction="always"
          android:title="@string/btn_connect"
          android:actionProviderClass="com.****.ConnectIconActionProvider"
          /&gt;
  &lt;/menu&gt;
  </code></pre>
  
  <p>layout/connecting_animation.xml</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;ImageView xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/ivConnecting"
      style="@android:style/Widget.ActionButton"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:src="@drawable/ic_not_connected" /&gt;
  </code></pre>
  
  <p>anim/connectinganimation.xml</p>
  
  <pre><code>&lt;animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false"&gt;
      &lt;item android:drawable="@drawable/ic_connecting1" android:duration="300" /&gt;
      &lt;item android:drawable="@drawable/ic_connecting2" android:duration="300" /&gt;
      &lt;item android:drawable="@drawable/ic_connecting3" android:duration="300" /&gt;
  &lt;/animation-list&gt;
  </code></pre>
  
  <p>ConnectIconActionProvider.java</p>
  
  <pre><code>public class ConnectIconActionProvider extends ActionProvider {
  
      private Context context;
      private ImageView button; 
  //  boolean toggle = false;
      private AnimationDrawable animationDrawable;
  
      public ConnectIconActionProvider(Context context) {
          super(context);
          this.context = context;
      }
  
      @Override
      public View onCreateActionView(MenuItem forItem) {
  
          // Inflate the action view to be shown on the action bar.
          LayoutInflater layoutInflater = LayoutInflater.from(context);
          View view = layoutInflater.inflate(R.layout.connecting_animation, null);
          button = (ImageView) view.findViewById(R.id.ivConnecting);
          button.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  MyClass.toggleConnectionToDevice(button);
  
  //              if (toggle) {
  //                  button.setImageResource(R.anim.connectinganimation);
  //                  animationDrawable = (AnimationDrawable) button.getDrawable();
  //                  animationDrawable.start();
  //              } else {
  //                  button.setImageResource(R.drawable.ic_not_connected);
  //                  if (animationDrawable != null) {
  //                      animationDrawable.stop();
  //                  }
  //              }
  //              toggle ^= true;
              }
          });
          return view;
      }
  </code></pre>
  
  <p>MyClass.toggleConnectionToDevice</p>
  
  <pre><code>public void toggleConnectionToDevice(ImageView iv) {
      if (deviceConnected) {
          (new DisconnectProgressBar(this, iv)).execute();
      } else {
          (new ConnectProgressBar(this, iv)).execute();
      }
  }
  </code></pre>
  
  <p>ConnectProgressBar.java</p>
  
  <pre><code>public class ConnectProgressBar extends AsyncTask&lt;Void, Void, Void&gt; {
  
      private final MainActivity activity;
      private AnimationDrawable animationDrawable;
      private ImageView iv;
      private Handler handler = new Handler(Looper.getMainLooper());
  
      public ConnectProgressBar(final MainActivity activity, final ImageView iv) {
          this.activity = activity;
          this.iv = iv;
      }
  
      @Override
      protected void onPreExecute() {
          if (iv != null) {
              iv.setImageResource(R.anim.connectinganimation);
              animationDrawable = (AnimationDrawable) iv.getDrawable();
              animationDrawable.start();
          }
      }
  
      @Override
      protected Void doInBackground(final Void... params) {
          // Connect to Car
          handler.post(new Runnable() {
              public void run() {
                  activity.isCurrentlyConnecting = true;
                  activity.connect(); 
              }
          });
          return null;
      }
  
      @Override
      protected void onPostExecute(final Void result) {
          if (activity.deviceConnected) {         
              // Show Connected Icon
              if (animationDrawable != null) {
                  animationDrawable.stop();
              }
              if (iv != null) {
                  iv.setImageResource(R.drawable.ic_connected);
              }
  
          } else {
              Toast.makeText(activity, "Connect failed!", Toast.LENGTH_LONG).show();
  
              // Show Disconnected Icon
              if (animationDrawable != null) {
                  animationDrawable.stop();
              }
              if (iv != null) {
                  iv.setImageResource(R.drawable.ic_not_connected);
              }
          }
      }
  }
  </code></pre>