Josh
Josh

Reputation: 2725

Android: Change ActionBar Menu Items from Fragment

Can anyone give a quick example of how to change the contents of an Activity action bar based on something that takes place in a fragment? My intent:

Normal menu items -> Something in the fragment is moved -> menu items change to save / discard buttons.

My first impulse is to setup Broadcast Receivers in both the activity and the fragment to cross talk, but I am not sure if this is correct.

Upvotes: 16

Views: 18348

Answers (4)

Abhishek J
Abhishek J

Reputation: 2584

Not sure if an ActionBar instance would help with the menu you but would surely be useful.. Here's a way to get about it

Try this to get the ActionBar from the FragmentActivity using the onAttach(Activity activity) method in the Fragment.

First of all make a global object of your FragmentActivity in the Fragment like this

public class YourFragment extends Fragment {

  private YourFragmentActivity context;
}

Override this in the YourFragment class

@Override
public void onAttach(Activity activity){
    context = (YourFragmentActivity)activity;
    super.onAttach(activity);
}

Then in the OnCreate method in the YourFragment do this

@Override
public View onCreateView(LayoutInflater inflater,
                         ViewGroup container, Bundle savedInstanceState){
    ...

    android.support.v7.ActionBar actionBar = context.getSupportActionBar();

    ...
}

Upvotes: 1

Sreejith B Naick
Sreejith B Naick

Reputation: 1188

Fragments can change menu in actionbar. For that you have to add necessary flag in fragment's oncreate() using method setHasOptionsMenu(true);

When your fragment is loaded you will get call at onCreateOptionsMenu(Menu menu, MenuInflater inflater) just like in an activity. Then do necessary changes to your menu.

Save you menu as global in fragment, and whenever you want to make a change, apply on it.

Upvotes: 23

David Smith
David Smith

Reputation: 910

I think you want to use a contextual action mode. On the drag event, you will start a new ActionMode which can replace the contents of the action bar with menu items specific to what you want to allow the user to do. Once the user chooses an action, you finish the action mode and the action bar returns to its previous state.

Upvotes: 4

Chris Lacy
Chris Lacy

Reputation: 4462

The following works for me. I have a custom class that implements ListView.MultiChoiceModeListener inside a Fragment:

public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {
    // Choose the correct Action Bar menu to display
    int menu = myCondition == true ? R.menu.my_default_menu : R.menu.my_menu_2;

    // Configure to use the desired menu
    mode.getMenu().clear();
    MenuInflater inflater = getActivity().getMenuInflater();
    inflater.inflate(menu);
}

Given how you detect 'something in the fragment has moved', extending ListView.MultiChoiceModeListener may not work for you, but hopefully this illustrates how to change the menu. The key is to get access to a ActionMode instance.

Upvotes: 7

Related Questions