0

I would like to show ProgressDialog during another asynctask.

I write ProgressDialog . one AsyncTask and I write another AsyncTask in PostExecute method.

but ProgressDialog doesn't run... I'd appreciate your kind help.

AsyncTask code:

public class SavingProgressTask extends AsyncTask<Void, Void, Void> {

    private Context mContext;
    private ProgressDialog progDialog = null;

    public SavingProgressTask(Context context) {
        mContext = context;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progDialog = new ProgressDialog(mContext);
        progDialog.setMessage("saving...");
        progDialog.setIndeterminate(false);
        progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progDialog.setCancelable(false);
        progDialog.show();
    }

    @Override
    protected Void doInBackground(Void... params) {
        return null;
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);

        if(isUpdateStore) {
            try {
                int storeResponseMessage = new StoreUpdateTask(SettingActivity.this).execute(fc_code).get();
                if(storeResponseMessage == 1) {
                    Store updateStore = new StoreDetailTask(SettingActivity.this, false).execute(fc_code).get();
                    mAuthUser.setStore(updateStore);
                    UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
                }
            } catch (InterruptedException e) {
                e.printStackTrace();
            } catch (ExecutionException e) {
                e.printStackTrace();
            }
        }

        int userinfoResponseMessage;
        int userinfosex;
        if(mSexTextView.getText().toString().equals("남자")) {
            userinfosex = 1;
        } else if (mSexTextView.getText().toString().equals("여자")){
            userinfosex = 2;
        } else {
            userinfosex = 2;
        }

        if(isUpdateBirthday) {
            if(isUpdateSex) {
                try {
                    userinfoResponseMessage = new UserUpdateTask(SettingActivity.this, true, "TWICE").execute(tempBirthDay, String.valueOf(userinfosex)).get();
                    if(userinfoResponseMessage == 1) {
                        mAuthUser.getUser().setBirthday(tempBirthDay);
                        mAuthUser.getUser().setSex(userinfosex);
                        UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            } else {
                try {
                    userinfoResponseMessage = new UserUpdateTask(SettingActivity.this, true, "BIRTHDAY").execute(tempBirthDay).get();
                    if(userinfoResponseMessage == 1) {
                        mAuthUser.getUser().setBirthday(tempBirthDay);
                        UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            }
        } else {
            if(isUpdateSex) {
                try {
                    userinfoResponseMessage = new UserUpdateTask(SettingActivity.this, true, "SEX").execute(String.valueOf(userinfosex)).get();
                    if(userinfoResponseMessage == 1) {
                        mAuthUser.getUser().setSex(userinfosex);
                        UserAuthUtil.saveUserObject(SettingActivity.this, mAuthUser);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                } catch (ExecutionException e) {
                    e.printStackTrace();
                }
            } else {
            }
        }

        if(progDialog != null) {
            progDialog.dismiss();

            Intent intent = new Intent(mContext, HomeActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            mContext.startActivity(intent);
        }
    }

in click listener:

SavingProgressTask saveTask = new SavingProgressTask(SettingActivity.this);
saveTask.execute();
0m3r
  • 12,286
  • 15
  • 35
  • 71
LEETAEJUN
  • 186
  • 9

1 Answers1

0

As far as I know starting another async task from the post execute of previous async task is not that good idea.

When I had a similar problem in one of my projects I had followed the following process.

1) Create an interface with a method like notifyUIThread().

2) Implement this interface from the activity, which calls the async task.

3) While creating the async task, pass 'this' as a parameter to the constructor of the async task.

4) In the post execute of async task call the this.notifyUIThread() method.

5) In the overridden method notifyUIThread(), make a call to the next async task.

Coming to the progress dialog, make it global variable, show it in the pre execute method of the first async task, dismiss it in the post execute of the second async task.

You can also do make the progress dialog local to the async task, show in pre execute and dismiss in post execute of both the async tasks.

But a small advice here is, keep the app responsive. Showing the progress dialog for a long time is not advisable.