Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the actionbar menu state depending on fragment

Tags:

android

I am trying to show/hide items in my action bar depending on which fragment is visible.

In my MainActivity I have the following

/* Called whenever invalidateOptionsMenu() is called */
@Override
public boolean onPrepareOptionsMenu(Menu menu) {

    if(this.myFragment.isVisible()){
        menu.findItem(R.id.action_read).setVisible(true);
    }else{
        menu.findItem(R.id.action_read).setVisible(false);
    }

    return super.onPrepareOptionsMenu(menu);
} 

This works great however, when the device is rotated there is a issue. After the rotation is complete onPrepareOptionsMenu is called again however this time this.myFragment.isVisible() returns false...and hence the menu item is hidden when clearly the fragment is visible (as far as whats shown on the screen).

like image 394
Jase Whatson Avatar asked Aug 14 '13 03:08

Jase Whatson


1 Answers

Based on the Fragments API Guide, we can add items to the action bar on a per-Fragment basis with the following steps: Create a res/menu/fooFragmentMenu.xml that contains menu items as you normally would for the standard menu.

<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/newAction"
        android:orderInCategory="1"
        android:showAsAction="always"
        android:title="@string/newActionTitle"
        android:icon="@drawable/newActionIcon"/>
</menu>

Toward the top of FooFragment's onCreate method, indicate that it has its own menu items to add.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    ...
}

Override onCreateOptionsMenu, where you'll inflate the fragment's menu and attach it to your standard menu.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.fooFragmentMenu, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

Override onOptionItemSelected in your fragment, which only gets called when this same method of the host Activity/FragmentActivity sees that it doesn't have a case for the selection.

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
        case R.id.newAction:
            ...
            break;
    }
    return super.onOptionsItemSelected(item);
}
like image 181
es0329 Avatar answered Nov 04 '22 08:11

es0329