I am trying to do some tasks in background with AsynTask
. As below:
class CheckTypesTask extends AsyncTask<Void, Void, Void> {
ProgressDialog Asycdialog = new ProgressDialog(Home.this);
String typeStatus;
@Override
protected void onPreExecute() {
super.onPreExecute();
Asycdialog.setMessage("First time initialization. Please wait...");
Asycdialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
create_database_and_tables();
insert_in_questions();
insert_in_answer_option();
return null;
}
@Override
protected void onPostExecute(Void result) {
// hide the dialog
super.onPostExecute(result);
database.close();
Asycdialog.dismiss();
}
Now the problem is that, if i press the hard back key while ProgressDialog
is visible, it simply just closes the ProgressDialog
. But when i check the Database
, i find that all of my records which are to be inserted, are inserted successfully. I dont understand why the ProgressDialog
keeps on showing even if the doInBackground()
finishes its work, no matter the ProgressDialog
disappears after sometime, but why is it so ??
My next question is that, if the data set that is to be inserted is not big enough, then why ProgressDialog
still appears. Actually, i tested like this. I pressed the hard back key immediately after two seconds. And checked the database, I found that all the records were inserted. Why onPostExecute()
is not called as soon as the doInBackground()
finishes its work ??
And the last question is, i want to show an AlertDialog
with yes and no Button
, when hard back key is pressed while ProgressDialog
is still visible. Then if, yes is pressed the ProgressDialog
should disappear and the doInBackground()
work should be stopped. And if no is pressed, then only the AlertDialog
should be dismissed, and the ProgressDialog
should still be visible.
Please help me solving my doubts.