Android Back Button and Progress Dialog

前端 未结 9 2069
攒了一身酷
攒了一身酷 2021-01-31 03:24

I have an AsyncTask that shows a progressDialog whilst working (it calls runOnUiThread from within doInBackground to show the progress dialog).

<
9条回答
  •  猫巷女王i
    2021-01-31 03:39

    First, you should show your dialog from OnPreExecute, hide it in OnPostExecute, and - if necessary - modify it by publishing progress. (see here)

    Now to your question: ProgressDialog.show() can take a OnCancelListener as an argument. You should provide one that calls cancel() on the progress dialog instance.

    example:

        @Override
        protected void onPreExecute(){
            _progressDialog = ProgressDialog.show(
                    YourActivity.this,
                    "Title",
                    "Message",
                    true,
                    true,
                    new DialogInterface.OnCancelListener(){
                        @Override
                        public void onCancel(DialogInterface dialog) {
                            YourTask.this.cancel(true);
                            finish();
                        }
                    }
            );
        }
    

    where _progressDialog is a ProgressDialog member of YourTask.

    This class was deprecated in API level 26. ProgressDialog is a modal dialog, which prevents the user from interacting with the app. Instead of using this class, you should use a progress indicator like ProgressBar, which can be embedded in your app's UI. Alternatively, you can use a notification to inform the user of the task's progress. LINK

提交回复
热议问题