toolbar menu item click in fragments

后端 未结 4 1933
鱼传尺愫
鱼传尺愫 2021-01-01 17:45

I know this question has been asked and but I am unable to solve my problem after looking at those answer. I have an Activity with a Fragment. In fragment, I have added the

4条回答
  •  南方客
    南方客 (楼主)
    2021-01-01 18:22

    From Android Docs, for Fragment:

    public void onCreateOptionsMenu (Menu menu, MenuInflater inflater)

    Initialize the contents of the Activity's standard options menu. You should place your menu items in to menu. For this method to be called, you must have first called setHasOptionsMenu(boolean).

    so you want to control actionbar menu in Fragment, then you have to call setHasOptionsMenu method better in Fragment's onCreate(...):

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

    also important is that you should not use inflateMenu and onCreateOptionsMenu both together for same ToolBar, inflateMenu is for standalone without setting ToolBar as ActionBar.


    Suggestion:

    keep one ToolBar in Activity as ActionBar for your App, and another ToolBar standalone inside Fragment.

    Hope this help!

提交回复
热议问题