问题
I developed an Application which offers a detail view Activity (An activity which is called when clicking on a list item to display further information). This details Activity shall display a "favorite" start in the Action Bar (it is yellow if the item is a favorite, transparent if not - for this purpose I have to different image resources)
If i click the menu item, the item is added / removed form the user's favorites:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
...
} else if (item.getItemId() == R.id.action_no_favorite) {
...
new FavoriteTask(DetailsActivity.this).execute(user, item);
mMenu.findItem(R.id.action_favorite).setVisible(true);
mMenu.findItem(R.id.action_no_favorite).setVisible(false);
} else if (item.getItemId() == R.id.action_favorite) {
...
new NoFavoriteTask(DetailsActivity.this).execute(user, item);
mMenu.findItem(R.id.action_favorite).setVisible(false);
mMenu.findItem(R.id.action_no_favorite).setVisible(true);
}
return super.onOptionsItemSelected(item);
}
This works without any problem.
But if the user opens the detail view, the Action Bar menu item needs to be updated immediately, depending on if this item is already a favorite or not. I solved this issue by overriding onCreateOptionsMenu:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.details_activity_actions, menu);
this.mMenu = menu;
...
new FetchFavortieStatusTask().execute(user, item);
return super.onCreateOptionsMenu(menu);
}
FavoriteStatusTask delivers a simple response (if the item is a favorite or not) and works like a charm. In onPostExecute, I update the menu item:
@Override
public void onPostExecute(String result) {
super.onPostExecute(result);
if (mMenu != null) {
if (result.equals("no favorite")) {
...
mMenu.findItem(R.id.action_favorite).setVisible(false);
mMenu.findItem(R.id.action_no_favorite).setVisible(true);
} else {
...
mMenu.findItem(R.id.action_favorite).setVisible(true);
mMenu.findItem(R.id.action_no_favorite).setVisible(false);
}
}
}
The problem that occurs: If I open a list item to get the detail view, the icon is changed after a delay of ~3 seconds or immediately if I touch the screen. The delay is definetly not caused by the AsyncTask, the result is delivered way faster. If I execute FetchFavortieStatusTask() in the end of the Activity's onCreate method, it works as expected most of the time (but still, sometimes I do have this delay). Moving the AsyncTask to onPrepareOptionsMenu doesn't change anything.
回答1:
ci_'s comment is a good hint but I had to adapt the solution in a bit different way (based on this answer: Is supportInvalidateOptionsMenu() working?)
I call the AsyncTask in onCreate() now and in its onPostExecute method I only set a member variable mIsFavorite to true or false and then execute supportInvalidateOptionsMenu() which calles onPrepareOptionsMenu a second time (but not onCreateOptionsMenu!). This means that I have to update the menu items in onPrepareOptionsMenu, based on the value of mIsFavorite.
来源:https://stackoverflow.com/questions/29239804/updating-action-bar-menu-item-due-to-asynctask-result-without-delay