Updating the ActionBar refresh spinner in onResume()
-
28-10-2019 - |
문제
We're running into a really painful issue with Honeycomb's ActionBar
here, and I was wondering if anyone else has found a solution. The problem itself is quite simple:
We frequently trigger background tasks in Activity onCreate
or onResume
, since most of our screens must display data loaded from a Web service. While these tasks are running, we show the ActionBar's refresh spinner and animate it. The problem is: onCreate and onResume are executed before onCreateOptionsMenu
is, so at the point where we start the task, the refresh spinner doesn't even exist. Hence, it won't animate.
I don't see any simple way around this. I guess we could keep the information about a task being spawned in onCreate, and as soon as the ActionBar becomes available, update it accordingly. But still, I can't believe a use case as common as this is so difficult to do with ActionBar?
해결책
So I tried using invalidateOptionsMenu
as outlined in question How to update a menu item shown in the ActionBar?, but that didn't have any effect.
I ended up tracking pending updates to the action bar using a simple boolean flag. Whenever there are calls to refresh its state, I set the flag, then check and reset it once the bar is created and perform any pending updates. Works great.