title: Android wait AsyncTask to finish
body: <p>I have a function, <code>AppHelper.isOnline(Context context)</code>, I call in various parts of my application to check that a session didn't timeout before making an HTTP request.</p>
  
  <pre><code>public void onClick(View v) {
          Intent intent = null;
          switch (v.getId()) {
  
          case R.id.buttonPagamenti:
              if (AppHelper.isOnline(this))
              {
                  //here AppHelper.isOnline should have finished it's async task
                  intent = new Intent(this, OrdineCreaActivity.class);
                  this.startActivityForResult(intent, R.id.buttonPagamenti);
              }
              break;
  ...
  </code></pre>
  
  <p>Inside <code>AppHelper.isOnline()</code>, I am executing an <code>AsyncTask</code> that logs in, thus making a network request, which can't be run on UI because otherwise I get an exception. I need to wait for it to finish BEFORE resuming with the code inside the if. How can I do this ?</p>
  
  <p>Problem is the activity starts firsts, then the <code>AsyncTask</code> executes, so when the activity expects a valid logged in session, it breaks.</p>