Question

I am wondering how I should solve one issue.

When I start an Activity, I download some data using an AsyncTask.

When the user selects an option on the menu, I use the cached data.

What if he selects the options while data is still downloading? What is the best way to wait for the data without blocking the UI?

edit: I don't want to change the menu/make the option disabled. I want to show a spinner while waiting for the data to finish its download.

Thanks

Was it helpful?

Solution

Disable/enable the items in the onMenuOpened() :

@Override
  public boolean onMenuOpened(int feature, Menu menu) {
    if (menu != null) {
      menu.getItem(INDEX_FOR_THE_MENU_ITEM).setEnabled(stuffIsAvailable);
    }
    return super.onMenuOpened(feature, menu);
  }

EDIT - showing a progress dialog:

ProgressDialog dialog = new ProgressDialog(context);
dialog.setMessage(context.getString(dialogTextResourceId));
dialog.show();

Don't forget to dialog.dismiss() it when you're done.

OTHER TIPS

You can call AsyncTask.getStatus() when the menu option is pressed and if it is still running then display the ProgressDialog.

Then in AsyncTask.onPostExeute() you can tell your Activity the data is available and dismiss the spinner.

Run an uncancellable progress dialog when the download is initiated, dismiss once the download is done. Read up on the ProgressDialog class.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top