简体   繁体   English

Android:调用onCreateOptionsMenu,但未在片段中更新操作栏菜单

[英]Android: onCreateOptionsMenu is called but actionbar menu isn't updated in fragment

I am using drawerlayout in activity with several fragments. 我在有几个片段的活动中使用drawerlayout By clicking the navigation item, I switch the fragment this way: 通过单击导航项,我以这种方式切换片段:

switch (index) {
            case 0:
                if (checkinFragment == null) {
                    checkinFragment = new CheckinFragment();
                    ft.add(R.id.main_container, checkinFragment, "0");
                } else {
                    ft.show(checkinFragment);
                }

                break;
            case 3:
                if (jianGuanFragment == null) {
                    jianGuanFragment = new JianGuanFragment();
                    ft.add(R.id.main_container, jianGuanFragment, "3");
                } else {
                    ft.show(jianGuanFragment);
                }

                break;
        }

        ft.commitAllowingStateLoss();

Each fragment has a different menu resource file inflated in its onCreateOptionsMenu(Menu menu, MenuInflater inflater) method. 每个片段在其onCreateOptionsMenu(Menu menu, MenuInflater inflater)方法中都有一个不同的菜单资源文件。

Steps: 脚步:
1. When first enter the activity the menu in checkinFragment is shown.(All right) 1.首次进入活动时,显示checkinFragment的菜单。(所有)
2. Then switch to the jianGuanFragment , the menu in jianGuanFragment is shown.(Also right) 2.然后切换到jianGuanFragment ,在菜单jianGuanFragment被示出。(另外右)
3. However, when switch back to the first fragment( checkinFragment ), the actionbar menu isn't updated. 3.但是,当切换回第一个片段( checkinFragment )时,操作栏菜单不会更新。 The showing menu is still the one in jianGuanFragment . 显示的菜单仍然是jianGuanFragment菜单。 That's the problem. 那就是问题所在。

And from the log I know the onCreateOptionsMenu in the showing fragment is called each time I switch to it. 从日志中,我知道每次切换到该片段时都会调用该显示片段中的onCreateOptionsMenu That's to say: 就是说:

onCreateOptionsMenu is called but actionbar menu isn't updated. 会调用onCreateOptionsMenu,但不会更新操作栏菜单。

Anyone can help me? 有人可以帮助我吗? Thanks a lot. 非常感谢。

EDIT 1: add the code in onCreateOptionsMenu: 编辑1:在onCreateOptionsMenu中添加代码:
(1)in checkinFragment (1)在检入片段中

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

        LogUtils.e("TAG", "CheckinFragment onCreateOptionsMenu");
        menu.clear();
        inflater.inflate(R.menu.menu_setting, menu);
    }

(2)in jianGuanFragment (2)在剑管碎片中

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

        LogUtils.e("TAG", "JianGuanFragment onCreateOptionsMenu");
        menu.clear();
        inflater.inflate(R.menu.menu_jianguan, menu);
    }

EDIT 2: I use toolbar, not the old actionbar. 编辑2:我使用工具栏,而不是旧的动作栏。

Use invalidateOptionsMenu() . 使用invalidateOptionsMenu() You need to use it where call to OnCreateOptionsMenu() is required. 您需要在需要调用OnCreateOptionsMenu()地方使用它。

EDIT: getActivity().invalidateOptionsMenu() can be used if you want to call it from Fragment . 编辑:如果要从Fragment调用getActivity().invalidateOptionsMenu()则可以使用它。

Hope that helps!!! 希望有帮助!!!

Try this 1)in checkinFragment 在checkinFragment中尝试1)

 @Override
    public void onPrepareOptionsMenu(Menu menu) {
        menu.clear();
        LogUtils.e("TAG", "CheckinFragment onCreateOptionsMenu");
        getActivity().getMenuInflater().inflate(R.menu.menu_setting, menu);
        super.onPrepareOptionsMenu(menu);
   }

At last I find the solution: 最后我找到了解决方案:

Each time entering a fragment, re-set the toolbar as ationbar again. 每次输入片段时,再次将工具栏重新设置为ationbar。

@Override
public void onHiddenChanged(boolean hidden) {
    super.onHiddenChanged(hidden);

    if (!hidden) {
        AppCompatActivity compatActivity = (AppCompatActivity) mActivity;
        mToolbar = (Toolbar) compatActivity.findViewById(toolbarId);
        compatActivity.setSupportActionBar(mToolbar);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM