Avoid locking the screen while ProgressDialog is displaying

android, android-asynctask

Solution

Dialogs in Android are usually modal. You can try creating the dialog then adding FLAG_NOT_TOUCH_MODAL to its window parameters - but I'm not sure if it will work. The idea behind it will be something like this:

ProgressDialog dialog = new ProgressDialog(this);
dialog.setMessage(...);
dialog.setTitle(...);
dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);

Note at the same time that it may be easier to just add a `ProgressBar` to your activity's layout and show it when needed instead of creating a dialog - this is, in fact, what Google documentation recommends. Android developer's guide on dialogs has this to say:

Avoid ProgressDialog

Android includes another dialog class called `ProgressDialog` that shows a dialog with a progress bar. However, if you need to indicate loading or indeterminate progress, you should instead follow the design guidelines for `Progress & Activity` and use a `ProgressBar` in your layout.

Problem

I want to show a progressBar while some instructions execute in background, but I don't want to lock the screen. This code is locking the screen: ``` private class ConvertDataInBackground extends AsyncTask<Params, Void, Boolean> { private MyProgressDialog mProgressDialog; @Override public void onPreExecute() { if(mProgressDialog == null) mProgressDialog = MyProgressDialog.show(mActivity, null, null); } @Override protected Boolean doInBackground(Params...params) { boolean conversionResult = executeConversion(params[0].item1, params[0].item2, params[0].item3); return conversionResult; } @Override protected void onPostExecute(Boolean result) { try { if(mProgressDialog.isShowing()) mProgressDialog.dismiss(); if(result) { Toast toast = Toast.makeText(mActivity, R.string.conversion_result, Toast.LENGTH_SHORT); toast.setGravity(Gravity.CENTER, 0, 0); toast.show(); } } catch (Exception e) { e.printStackTrace(); } } } ``` Execution of instruction in background may take a while, and I want the user to be able to work with the interface. Do you know anyway that we can work with interface, while progressDialog is displaying at the top?

Original source