Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Back Button and Progress Dialog

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

Whilst its running I want to allow the use of the back button to cancel the operation; someone else has had this problem: BACK Button is not working ,while progressDialog is running

For what ever reason I can't reply to that thread, hence having to start another?! (Another question for another day)

I had the same idea as Sandy but this code is never called whilst the progressDialog is showing, why is this? I have implemented it inside my main activity class, does the progressDialog take the foreground focus away from my class temporarily?

like image 332
jwbensley Avatar asked Mar 09 '11 23:03

jwbensley


People also ask

What is Progress dialogue?

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.

What can I use instead of ProgressDialog?

ProgressBar is best alternative for ProgressDialog.

How do you make progress dialog?

ProgressDialog dialog = new ProgressDialog(MainActivity. this); dialog. setMessage("Your message.."); dialog. show();


2 Answers

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

like image 117
user634618 Avatar answered Oct 05 '22 08:10

user634618


This can be achieved by the following code fragment:

progress.setCancelable(true); progress.setCanceledOnTouchOutside(false); 

progress is the ProgressDialog object...

This will enable the back button to close the dialog but prevent any touch input to do that...

like image 40
Jahid Avatar answered Oct 05 '22 08:10

Jahid