2

I have a ListView Activity that uses a AsyncTask to load data from the database. This Activity has an options menu that checks to see if there are any data items in the Activities ListView in onPrepareOptionsMenu().

If there are items, I enable one of the options that is shown on the ActionBar that allows the user to delete the items.

Now, when the Activity starts, the AsyncTask starts and as onPrepareOptionsMenu() is run through while the AsyncTask is still running, this menu item is never enabled, unless the device is flipped and the listview data gets passed in as an instance, bypassing the AsyncTask.

So, in the AsyncTask's onPostExecute(), I call invalidateOptionsMenu() but that does not seem to be the menu to refresh (I have debugging code in onCreateOptionsMenu() and onPrepareOptionsMenu(), and neither is fired). Any help appreciated.

Unpossible
  • 10,607
  • 22
  • 75
  • 113

2 Answers2

1

You can try creating a global boolean like haveData on your Activity and upon onPostExecute(), set the boolean to true or false accordingly. Then based on the boolean do a check on your onPrepareOptionsMenu() and enable the menu item accordingly.

The last time i tried it worked for me not sure why it didn't for you. Do a debug from there if it doesn't work tell us where it isn't executing.

Rejinderi
  • 11,694
  • 2
  • 31
  • 40
1

The issue was related to a bad global variable value. I was gatewaying the code inside the onPrepareOptionsMenu() method with a global boolean value that was never true, and had put the debugging statement INSIDE the boolean check... resulting in the debugging statement never running.

After addressing that issue, everything is working as expected, code above is correct.

Unpossible
  • 10,607
  • 22
  • 75
  • 113