Trying to do the following (code is in the main activity):
private MenuItem mainMenuSync = null;
public void onCreate(Bundle savedInstanceState) {
..开发者_如何学Python.
mainMenuSync = (MenuItem) findViewById(R.id.main_menu_sync);
}
private class Synchronize extends AsyncTask<Void, Void, Void> {
...
@Override
protected void onPreExecute() {
mainMenuSync.setEnabled(false);
}
But it fails. What I am doing wrong? (This menu item really starts this ASyncTask and I would like to avoid starting it again)
Since your mainMenuSync
is coming from a Main Menu, I guess you are creating it in onCreateOptionsMenu, isn't it?
If that's the case, filling its value using onCreate
would have no effect, since by that moment, your main menu isn't inflated from the xml yet, and I wonder you should already FC before seeing anything on the screen.
Main Menu is not the same as other UI item in Android, which in most use cases they are inflated on demand in onCreateOptionsMenu
and reused next in onPrepareOptionsMenu
. What you can do is to declare a flag to enable/disable that item:
boolean showMainMenuSync = true;
onPrepareOptionsMenu(Menu menu){
super.onPrepareOptionsMenu(menu);
menu.findItem(R.id.main_menu_sync).setEnabled(showMainMenuSync);
}
which of course showMainMenuSync
should be changing by your async task.
精彩评论