AsyncTask not working with ProgressDialog in android
i am downloading data from website using asynctask
and
my code for async task is below
public class getSyncTaskInBackground extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
getSynchronizeTask();
return null;
}
@Override
protected void onPostExecute(Void result) {
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
displaySynchronizetask();
super.onPostExecute(result);
}
@Override
protected void onPreExecute() {
progressDialog = ProgressDialog.show(synchronize.this, "Tasks are synchroning...", "Please wait...");
super.onPreExecute();
}
@Override
protected void onCancelled() {
super.onCancelled();
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
}
this thread takes more time to complete task
so if i want to cancel this thr开发者_运维知识库ead in between of process then i had write this code of back button pressed
or if progressbar is already dismiss then i want to close activity by calling finish();
my code for back button is as below
@Override
public void onBackPressed() {
syncThread.cancel(true); //syncThread is the object of getSyncTaskInBackground
if(progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
} else {
finish();
}
}
Now when i pressed back button then the progressdialog
is not dismiss
is any mistake in mycode ? is any way to complete my need ?
please help me
Hi
You might want to call
setCancelable with true on your progressDialog instance :
http://developer.android.com/reference/android/app/Dialog.html#setCancelable(boolean)
If you want an event on the cancelation of your progressDialog
you can set an onCancelListener
http://developer.android.com/reference/android/app/Dialog.html#setOnCancelListener(android.content.DialogInterface.OnCancelListener)
hope this helps
精彩评论