Is there a method in the Android SDK that allows me to disable all UI-Elements such as Buttons and MenuItems while I perform a task in the background?
I currently do some work over the internet and display a ProgressBar informing the user about the current progress.
Should I just build a List<> of all my Buttons and MenuItems and update their state at the start and end of the background task via the setEnabled method?
I don’t recall of such a method being present in the SDK.
My advice is to use a
ProgressDialoginstead of your current approach(the dialog will also prevent the users from interacting with the UI, you’ll just have to disable the menu). If you start the background worker and disable all the UI views the user will see that there is some work being done and also that he/she can’t use the app. For me this seems confusing and aDialogseems a much better solution as it clearly indicates that the user must wait until the work is done. Another option is to place theProgressBarin a overlay that will cover the activity so the user can still see the below UI if this is what you want.To disable all the views I would use a method like the one below:
passing the system’s content panel (
changeViewsState((ViewGroup) findViewById(android.R.id.content), false);) or the root of your activity’s layout:To disable the menu items have a boolean flag that is set when the background work is about to begin and use that flag to enable/disable menu items in the callback
onPrepareOptionMenu. When the work finishes remember to revert the flag so the menu will be enabled.