Trying to do the following (code is in the main activity):
private MenuItem mainMenuSync = null;
public void onCreate(Bundle savedInstanceState) {
...
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
mainMenuSyncis 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
onCreatewould 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
onCreateOptionsMenuand reused next inonPrepareOptionsMenu. What you can do is to declare a flag to enable/disable that item:which of course
showMainMenuSyncshould be changing by your async task.