其他分享
首页 > 其他分享> > android中的onCreateOptionsMenu()片段不替换ActionBar菜单

android中的onCreateOptionsMenu()片段不替换ActionBar菜单

作者:互联网

我在我的应用程序中使用NavigationDrawer并在单击抽屉中的项目时替换片段.我的问题是,当我更改片段时,ActionBar中的菜单项不会更新.

我已经仔细阅读了本教程https://www.grokkingandroid.com/adding-action-items-from-within-fragments/,但它仍然无法在我的应用程序中运行.

在此处添加父活动和其中一个片段的一些代码片段.
我需要的是当初始片段被ContactFormFragment.java替换时,显示联系表单片段(R.menu.contactform_send_menu)的其他菜单项.

public class MainActivity extends FragmentActivity implements ActionBar.OnNavigationListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setTitle(R.string.app_name);

        // Set up the action bar to show a dropdown list.
        ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setHomeButtonEnabled(true);

        createNavigationDrawer(savedInstanceState);
    }

    private final int DRAWER_MAIN = 0;
    private final int DRAWER_CONTACT = 5;

    // update the main content by replacing fragments
    private void selectItem(int position) {
        Fragment fragment = null;
        Bundle args = new Bundle();
        boolean isFragment = false;

        switch (position) {
        case DRAWER_MAIN:
            fragment = new WelcomeSectionFragment();
            args.putString(WelcomeSectionFragment.ITEM_NAME, dataList.get(position).getItemName());
            args.putInt(WelcomeSectionFragment.IMAGE_RESOURCE_ID, dataList.get(position).getImgResID());
            getActionBar().setTitle(R.string.app_name);
            isFragment = true;
            break;
        case DRAWER_CONTACT:
            fragment = new ContactFormFragment();
            args.putString(ContactFormFragment.ITEM_NAME, dataList.get(position).getItemName());
            args.putInt(ContactFormFragment.IMAGE_RESOURCE_ID, dataList.get(position).getImgResID());
            getActionBar().setTitle(R.string.contactform_title);
            isFragment = true;
            break;
        default:
            break;
        }

        if (isFragment) {
            fragment.setArguments(args);
            FragmentManager frgManager = getFragmentManager();
            frgManager.beginTransaction().replace(R.id.content_frame, fragment).commit();

            mDrawerList.setItemChecked(position, true);
            setTitle(dataList.get(position).getItemName());
            mDrawerLayout.closeDrawer(mDrawerList);
        }
    }

}

和片段:

public class ContactFormFragment extends Fragment {

    public ContactFormFragment() {

    }

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

        ca = getActivity();
        Reachability.registerReachability(ca.getApplicationContext());

        settings = ca.getSharedPreferences(Konstanten.PREFERENCES_FILE, 0);
        member = new Gson().fromJson(settings.getString(Konstanten.MEMBER_OBJECT, null), Member.class);

        latoFontLight = Tools.getFont(ca.getAssets(), "Lato-Light.ttf");
        latoFontBold = Tools.getFont(ca.getAssets(), "Lato-Bold.ttf");

        // Update action bar menu items?
        setHasOptionsMenu(true);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        // Do something that differs the Activity's menu here
        super.onCreateOptionsMenu(menu, inflater);

        menu.clear();

        if (Build.VERSION.SDK_INT >= 11) {
            // selectMenu(menu);
            inflater.inflate(R.menu.contactform_send_menu, menu);
        }
    }
}

调试时我可以看到setHasOptionsMenu(true);在onCreate()中调用,我也进入ContactFormFragment.java的onCreateOptionsMenu().我只是不明白为什么操作栏保留其初始菜单项并且不替换它们.我错过了什么?

谢谢你的帮助.

解决方法:

尝试使用setHasOptionsMenu(true);在ChildFragment.java的onCreateView()中
我在你发布的代码中没有看到一个.

标签:android,android-fragments,android-actionbar,navigation-drawer,android-menu
来源: https://codeday.me/bug/20190716/1477395.html