ASH
ASH

Reputation: 75

Set ProgressDialog dismiss on Back button In Toolbar

I have ProgressDialog in my activity and I want to dismiss it when I click the BackButton of The Toolbar. I have added the OnKey Function to handle BackButton functionlity of Phone and that works Fine,but when I click BackButton on the Toolbar it doesn't work.

Public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (keyCode == KeyEvent.KEYCODE_BACK) {
                progressdialog.dismiss();
                finish();

            }
            return true;
        }

I have Added setOnclickListener method on BackButton of Toolbar but it only works when data is Fully Loaded from the database and when the ProgressDialog Dismisses to go back to previous activity.

Upvotes: 0

Views: 153

Answers (1)

Josef Adamcik
Josef Adamcik

Reputation: 5780

ProgressDilog is a modal dialog which means user cannon interact with application in any way. So it's not possible to tap on any button when dialog is displayed. Dialog can be dismissed by back button and you don't even need to subclass it and override the onKey method, you can just call correct static method for displaying .

You should probably consider other way to show progress to the user. ProgressDilaog is deprecated.

From the documentation:

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.

Upvotes: 1

Related Questions