TheLettuceMaster
TheLettuceMaster

Reputation: 15734

Altering ActionBar from Fragments

I need to make two minor changes to an Activity's ActionBar from a Fragment.

The parent FragmentActivity's onCreateOptionMenu and onPrepareOptions handle the menu across the app.

However, in the containing main Fragment view, I want the ActionBar title to change for the sake of navigation. Each time the Fragment changes.

Also, in one instance, I want to add a single menu item as defined in the layout. I rather not duplicate code for this part, do I have to override the same methods and set up entire menu again, including onOptionSelected? Or...?

Edit:

On my menu xml layout, I have this item:

 <item
        android:id="@+id/Add"
        android:title="Add Items"/>

This item needs to be seen in the ActionBar only for one particular Fragment. The rest of the menu is identical to what should be seen for the rest of the app. In the fragment where I need this new item, do I have to also do onCreateOptionsMenu, onPrepareOptionsMenu, and onOptionsItemSelected all over again and duplicate thee OTHER menu items as well? Or can I override those in the Fragment and just pop that one extra menu item in?

Upvotes: 0

Views: 300

Answers (2)

TheLettuceMaster
TheLettuceMaster

Reputation: 15734

This answered what I needed for part 2.

Putting this in onActivityCreated:

 setHasOptionsMenu(true);

And then adding this too:

 @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);

        ActivityCompat.invalidateOptionsMenu(getActivity());

        MenuItem Add = menu.findItem(R.id.Add);
        Add.setVisible(true);


    }

The parent Activity sets the R.id.Add as invisible by default.

Upvotes: 0

agamov
agamov

Reputation: 4427

1.

getActivity().getActionBar().setTitle();

or

getActivity.getSupportActionBar().setTitle();
  1. more clarification please :)

Upvotes: 1

Related Questions