title: doInBackground calling AsyncTask then sleeps blocks UI Thread
body: <p>I have an activity, composed of an AsyncTask aiming to launch a request when the user clicks on the button. I have been looking for answers, but I didn't find the same problem, or it didn't the same for me. The code is doing what I want, but the ProgressDialog looks blocked as the spinner is not turning sometimes (almost all the time).</p>
  
  <p>When I click on the button :</p>
  
  <p>AsyncTask is launched -> showDialog() is called onPreExecute -> startSearch ( SearchManager launches a new AsyncTask with in the doInBackground there is a heavy call with network ) -> doInBackground in Activity waits for SearchManager to be loaded -> display.</p>
  
  <p><strong>Code for button :</strong></p>
  
  <pre><code>button_search.setOnClickListener(new OnClickListener() {
              public void onClick(View v) {
                  new SearchTask().execute();
  
              }
          });
  </code></pre>
  
  <p><strong>Code for AsyncTask in Search Activity :</strong></p>
  
  <pre><code>private class SearchTask extends AsyncTask&lt;Void,Void,Void&gt;{
  
          @Override
          protected void onPreExecute(){
              showDialog(DIALOG_LOADING_ID);
  
              searchManager.startSearch();
          }
  
          @Override
          protected Void doInBackground(Void... params) {
  
  
              while(searchManager.isLoading()){
                  try {Thread.sleep(150);} catch(Exception e){};
              }
  
  
              return null;
          }
  
          @Override
          protected void onPostExecute(Void ret){
              try {dismissDialog(DIALOG_LOADING_ID);} catch (Exception e){};
  
              if ( searchManager.errorOccurred() ){
                  //Error
              } else {
                  //No Error
  
              }   
          }
  </code></pre>
  
  <p><strong>Code for SearchManagerAsyncTask : which is directly launched by startSearch</strong></p>
  
  <pre><code>protected class SearchAsync extends AsyncTask &lt;Void,Void,Void&gt;{
  
          @Override
          protected Void doInBackground(ComSearchAds... datas) {
              global.getDataManager().doSearch();
                          //... When finished
                          setIs_loading(false);
                  }
  }
  </code></pre>
  
  <p>I'm apparently doing something wrong, but can't find what and how to avoid this. Thanks for your help !</p>
  
  <p><strong>SOLUTION :</strong> </p>
  
  <p>Finally, it appears that the not spinning ProgressDialog was because I was using the same instance of ProgressDialog and </p>
  
  <pre><code>showDialog(DIALOG_LOADING_ID); 
  //doInBackground
  dismissDialog(DIALOG_LOADING_ID);
  </code></pre>
  
  <p>used with  causes problem, I changed to </p>
  
  <pre><code>removeDialog(DIALOG_LOADING_ID)
  </code></pre>
  
  <p>and now it's working fine. </p>
  
  <p>Thanks All, and hope it can help someone someday !</p>