Search code examples
javaandroidandroid-actionbarbroadcastreceiver

making ActionBar icon dynamically visible/invisible


My MainActivity calls an AsyncTask to do some network I/O. When the AsyncTask begins, I would like to 'light up' an icon in the ActionBar. When the AsyncTask completes, I would like to make that ActionBar icon invisible, i.e. upon AsyncTask completion, not via a button click.

At this point I can successfully light up the ActionBar icon upon AsyncTask start. At the completion of AsyncTask, I send another broadcast back to MainActivity to indicate that AsyncTask is completed. I need to be able to use this broadcast to make the ActionBar icon invisible. I am attempting to used InvalidateMenuOptions() to force onPrepareMenuOptions to recreate and make the ActionBar icon invisible, but it is not working. Here is my attempt so far - all code is excerpted from my MainActivity class

excerpt from MainActivity:

public class MainActivity extends Activity {
    private static boolean dataXferRunning = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ActionBar actionBar = getActionBar();
        actionBar.setHomeButtonEnabled(true);

        // broadcast receiver to determine if BasestationTransfer is active
        basestationBroadcastReceiver = new BasestationBroadcastReceiver();
        basestationIntentFilter = new IntentFilter("XFER_RUNNING_ACTION");

        boolean hasWifi = CWICUtil.getNetworkState(this);
        if(hasWifi) {
            if(dataXferRunning == false) {
                containerCount = 0;
                DBAdapter dbAdapter = CWICApplication.dbAdapter();
                try {
                    containerCount = (int)dbAdapter.getUnsentContainersCount();
                } catch (SQLException e) {
                    Log.e(TAG, "getUnsentContainersCount() failed: " + e.toString());
                    e.printStackTrace();
                }
                if(containerCount > 0) {
                    invalidateOptionsMenu();
                    startTransfer();  //AsyncTask
                    invalidateOptionsMenu();
                }
            }
        }
    }
}

my BroadcastReciever:

public static class BasestationBroadcastReceiver extends BroadcastReceiver {  
    @Override
    public void onReceive(Context context, Intent intent) {
        intent.getAction();
        boolean isRunning = intent.getBooleanExtra("XFER_RUNNING", false);
        if(isRunning) {
            dataXferRunning = true;
        } else {
            dataXferRunning = false;
        }
    }
};

my onPrepareOptionsMenu:

@Override
public boolean onPrepareMenuOptions(Menu menu) {
    if(dataXferRunning) {
        menu.findItem(R.id.action_socketServiceConnected).setVisible(true);
    } else {
        menu.findItem(R.id.action_socketServiceConnected).setVisible(false);
    }
    return super.onPrepareOptionsMenu(menu);
}

So to summarize - I'm successfully receiving broadcasts from my AsyncTask with correct values. I have seen much code that does this with a buttonClick, however my issue is dynamically making my ActionBar icon invisible upon completion of AsyncTask. Also - this is my first time posting, let me know if I can clarify any issues plus thanks to this community because you have been getting me out of jams for months now.


Solution

  • In onCreateOptionsMenu(), you can hold on to a reference of your Menu object (via a class variable, or some other mechanism of your choice), like this:

    Menu menu;
    
    @Override
    public boolean onCreateOptionsMenu( Menu menu ) {
      getSupportMenuInflater().inflate( R.menu.my_menu, menu );
      this.menu = menu;     
    
      return true;
    }
    

    Then, when you want to dynamically adjust the menu item's visibility, you can reuse that menu object reference to call menu.findItem(R.id.myId) to get your individual menu items and adjust their properties dynamically however you'd like.