title: Show a progress bar when an Activity is loading
body: <p>I have a <code>ListActivity</code> which launches another <code>Activity</code> based on the list selection.  This second <code>Activity</code> needs to load a fair bit of data from the internet and as such there is a noticeable delay between when the user clicks on an item and when the <code>Activity</code> displays.</p>
  
  <p>This is a problem because I currently have no way to indicate to the user that their click is being processed (even just changing the colour of the selected list item would be sufficient but I can't find a good way to do that).  Ideally I'd be able to display an indeterminate <code>ProgressDialog</code> while the second <code>Activity</code> is loading.</p>
  
  <p>I've tried a few different approaches for this but nothing seems to work as desired.  </p>
  
  <hr>
  
  <p>I've tried the following:</p>
  
  <ul>
  <li>Retrieving the serializable data (not all of it but some part) in an <code>AsyncTask</code> in the first <code>Activity</code> and passing it as an extra to the second. This didn't really work well as a <code>ProgressDialog</code> I created in <code>onPreExecute()</code> didn't display immediately (it seems delayed by the processing done in <code>doInBackground()</code> for some reason.)</li>
  </ul>
  
  <p>Here is the code for that:</p>
  
  <pre><code>AsyncTask&lt;String, Void, String&gt; read = new AsyncTask&lt;String, Void, String&gt;() {
      Dialog progress;
  
      @Override
      protected void onPreExecute() {
          progress = ProgressDialog.show(SearchActivity.this, 
                  "Loading data", "Please wait...");
          super.onPreExecute();
      }
  
      @Override
      protected String doInBackground(String... params) {
          DatasetReader reader = new DatasetReader();
          reader.setFundID(params[0]);
          reader.addDatsets(FundProfile.datasets);
          reader.populate();
          return reader.toString();
      }
  
      @Override
      protected void onPostExecute(String result) {
          super.onPostExecute(result);
          progress.dismiss();
      }
  };
  read.execute(selectedItem.getUniqueID());
  try {
      action = new Intent(SearchActivity.this, FundProfile.class);
      action.putExtra("data", read.get());
  } catch(Exception ex) {
      ex.printStackTrace();
  }
  </code></pre>
  
  <ul>
  <li><p>In the second Activity's <code>onCreate()</code> method (this does not work at all):</p>
  
  <pre>requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
  setProgressBarVisibility(true);</pre></li>
  </ul>
  
  <p>Here is the <code>onCreate()</code> method for the second approach:</p>
  
  <pre><code>public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      this.setTitleColor(Color.WHITE);
      requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
      setProgressBarVisibility(true);
  
      try {
          setContentView(R.layout.fund_profile);
          // init some data
          setProgressBarVisibility(false);
      } catch(Exception ex) {
          FundProfile.this.finish();
      }
  }
  </code></pre>