0

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 ProgressDialogstill 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.

jimpanzer
  • 3,470
  • 4
  • 44
  • 84
Chintan Soni
  • 24,761
  • 25
  • 106
  • 174

1 Answers1

0

If you place some line of code in doInBackground that only executed once and not in loop,there is no way to cancel it after executing your task.But if you have loop in it or other commands that run periodically,you can use setCancel(true) for your task and check the value of isCanceled() of task before doing your statement in loop.

I guess that when you close dialog,it takes some times to check DB records and during this timedoInBackground continues until all records inserted in DB.So when you check your DB,see all records are inserted.

Also onPostExecute() immediately being invoked on the UI thread after doInBackground(Object[]) returns(if task was not canceled).For example execute this AsyncTask:

class Test extends AsyncTask<Void, Void, Void>{

        @Override
        protected Void doInBackground(Void... params) {
            System.out.println("End doInBackground");
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            System.out.println("Start onPostExecute");
        }

    }

But you added some commands before dismissing dialog that may consumes little times and also Activity transitions(dismissing dialog and foregrounding Activity) consumes some times itself.

Edit:

And the last question is, i want to show an AlertDialog with yes and no Button,...

You have to add a method to your AsyncTask class that shows an alert dialog with desired functionality and store a reference to task and also override onBackPressed() method of Activity.When onBackPressed() executed,call that method of Task instance.Although you know you can not cancel inserting progress in this way.If you want that no record insert to DB,you can remove them in background, after they inserted!!

hasanghaforian
  • 13,858
  • 11
  • 76
  • 167