Reputation: 1074
I am trying to query a web service using loopJ and during this operation I want to show the users a progress dialog. When the operation is complete, I want the progress dialog to dismiss and start a new activity intent.
I know AsyncTask is the way to go. On onPreExecute method I show the progress dialog. On doInBackground I am performing the network operation. And onPostExecute I am dismissing the dialog and starting a new activity intent.
My issue is doInBackground will perform loopJ networkcall asynchronously so onPostExecute will finish first before my network operation. If you look at my logs it will show:
"Starting new activity!" "Fetched category services!"
rather
"Fetched category services!" "Starting new activity!"
How do I accommodate an asynchronous task running doInBackground? Is there a way in onPostExecute to wait till my asynch loopJ operation is done?
public class FetchCategoriesServices extends AsyncTask<HITECategory, String, String>
{
private Category userSelectedCategory;
private ProgressDialog busyDialog;
@Override
protected void onPreExecute()
{
busyDialog = ProgressDialog.show(SearchActivity.this, getApplicationContext().getString(R.string.progressDialogTitle),
getApplicationContext().getString(R.string.progressDialogMessage));
}
@Override
protected String doInBackground(HITECategory... params)
{
userSelectedCategory = params[0];
String requestCategoryServiceURL = BASE_URL + "GetServices?CategoryID=" + userSelectedCategory.categoryID + "&ResponseType=JSON";
try
{
Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler()
{
@Override
public void onSuccess(String jsonResponse)
{
Gson gson = new Gson();
CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class);
categoryServiceresults = Response.categoryServices;
Log.d(getString(R.string.DebugKey), "Fetched category services!");
}
});
}
catch (Exception e)
{
Log.d(getString(R.string.DebugKey), "Error connecting to service and fetching category services list");
}
return null;
}
@Override
protected void onPostExecute(String params)
{
busyDialog.dismiss();
Log.d(getString(R.string.DebugKey), "Starting new activity!");
Intent intent = new Intent(getApplicationContext(), CategoriesSearchActivity.class);
startActivity(intent);
}
}
Upvotes: 1
Views: 1189
Reputation: 2702
Just put the code in onPostExecute
into onSuccess
method:
Client.get(requestCategoryServiceURL, new AsyncHttpResponseHandler()
{
@Override
public void onSuccess(String jsonResponse)
{
Gson gson = new Gson();
CategoryServicesListResponse Response = gson.fromJson(jsonResponse, CategoryServicesListResponse.class);
categoryServiceresults = Response.categoryServices;
Log.d(getString(R.string.DebugKey), "Fetched category services!");
youractivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
busyDialog.dismiss();
Log.d(getString(R.string.DebugKey),
"Starting new activity!");
Intent intent = new Intent(getApplicationContext(),
CategoriesSearchActivity.class);
youractivity.this.startActivity(intent);
}
});
}
});
Upvotes: 2