title: Android button disappears after running AsyncTask
body: <p>I'm making an app as part of a project which connects to a device on a network (a specific device). After clicking a button, which calls an <code>AsyncTask</code>, the button disappears and is not clickable.</p>
  
  <p>I'm using the <a href="https://github.com/navasmdc/MaterialDesignLibrary/blob/master/MaterialDesign/src/com/gc/materialdesign/views/ButtonRectangle.java" rel="nofollow"><code>ButtonRectangle</code></a> class from this library: <a href="https://github.com/navasmdc/MaterialDesignLibrary" rel="nofollow">https://github.com/navasmdc/MaterialDesignLibrary</a></p>
  
  <p>The relevant code:</p>
  
  <pre><code>// ...
  
  int numAdded = addResultsToDatabase(items);
  if(!deviceInitTask.isCancelled() &amp;&amp; numAdded &gt; 0) {
      String devices = "device";
      if(numAdded &gt; 1) {
          devices = "devices";
      }
      Toast.makeText(NewLocalDeviceScanResultsActivity.this,
              "Added " + NumberToWords.convert(numAdded) + " " + devices,
              Toast.LENGTH_LONG).show();
  
      // go to device list activity
      Intent intent = new Intent(NewLocalDeviceScanResultsActivity.this, DeviceListActivity.class);
      startActivity(intent);
      finish();
  }
  else {
      // button disappears
  }
  
  // ...
  </code></pre>
  
  <p>The above code is inside an anonymous <code>OnClickListener</code> on the <code>ButtonRectangle</code> object. The <code>addResultsToDatabase</code> method is as follows:</p>
  
  <pre><code>// ...
  
  List&lt;String[]&gt; devices = new ArrayList&lt;&gt;();
  for(int i = 0; i &lt; items.size(); i++) {
      ScanResultsRecyclerViewAdapter.ListItem item = items.get(i);
      if(item.isSelected()) {
          String[] device = new String[4];
          device[0] = item.getDescription().trim();
          device[1] = item.getHost().trim();
          device[2] = item.getLoginUsername().trim();
          device[3] = item.getLoginPassword().trim();
  
          devices.add(device);
      }
  }
  
  // try connecting and get results
  deviceInitTask = new LocalDeviceInitTask(this, devices);
  deviceInitTask.execute();
  
  // ...
  </code></pre>
  
  <p>The <code>DeviceInitTask</code> simply tries to connect to the device, log in using the supplied username and password and returns an auth token and expiry if successful. If it cannot connect, <code>this.cancel(true)</code> is called from within the task and a Toast is shown.</p>
  
  <p>Note that I'm testing this so that logging in fails, so <code>numAdded == 0</code>.</p>
  
  <p>I'm at a loss as to what's causing it. The button still has <code>View.VISIBLE</code> after the above code has run. <code>button.invalidate()</code> doesn't work.</p>
  
  <p>The only thing that has worked so far is, obviously, recreating the activity, but this isn't ideal because user input is lost and it's just not good practice.</p>
  
  <p>Any advice?</p>
  
  <p>======================</p>
  
  <p>EDIT: so now instead of running a separate <code>AsyncTask</code> for each device, all the connecting is done in one task, and the results are handled by <code>onPostExecute</code> instead of <code>task.get()</code>. The end result is the same - the button vanishes.</p>
  
  <p>======================</p>
  
  <p>UPDATE: I've stepped through the code in debug mode and found that the button disappears after creating a <code>DefaultHttpClient</code> object (org.apache.http.impl.client). I'm going to step into that class and see if I can figure out where exactly it happens.</p>