Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I setSupportActionBar() in multiple fragments?

I have two support fragments sitting inside an AppCompatActivity. By design they each have unique Toolbar and Option Menus. The AppCompatActivity does not have a toolbar in it's layout as these are included in each fragment.

In each fragment, I setHasOptionsMenu(true); in onCreate().

In onCreateView() I am calling ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar); where toolbar is the object bound to the xml Toolbar element.

In onCreateOptionsMenu(), I first make the call to super, then I call menu.clear() and finally inflate the menu with inflater.inflate(R.menu.searchbar_menu, menu);

Where this fails is in calling ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar); in both fragments. In doing so, the options will only appear on the second fragment and not on the first. If I call it in only one fragment, the options appear as expected in only that one fragment. Of course, if I call it in neither, they do not appear at all.

Here is my code. Both Fragments are essentially identical with the exception of the menus inflated:

public class FeedFragment extends Fragment implements FeedView{

    @BindView(R.id.toolbar)
    Toolbar toolbar;

    FeedPresenter presenter;

    static final String TAG = "FEED_FRAGMENT";

    /*
     * Some boilerplate fragment setup code
     */

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

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

        View view = inflater.inflate(R.layout.fragment_feed, container, false);
        ButterKnife.bind(this, view);
        ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);
        setupToolar();
        return view;
    }

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

        menu.clear();
        inflater.inflate(R.menu.searchbar_menu, menu);

    }

    private void setupToolbar(){
        toolbar.setPadding(0,ScreenUtil.getStatusBarHeight(this.getActivity()),0,0);
        toolbar.setTitle("");
    }

    /*
     * Other logic code unique to each fragment
     */

Does anyone know has I can set both fragments to have their toolbar options?

Thanks in advance.

like image 653
John Riggs Avatar asked Nov 08 '22 20:11

John Riggs


1 Answers

When you call getActivity(), it returns the same instance of your Activity. The second fragment was initialized later than the first fragment, that why the Activity can only support action bar on the second fragment.

You could try this

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);
    if (isVisibleToUser) {
        AppCompatActivity baseActivity = (AppCompatActivity) getContext();
        baseActivity.setSupportActionBar(yourToolbar);
    }
}
like image 179
nhp Avatar answered Nov 15 '22 12:11

nhp