片段 xamarin 中未调用 OnOptionsItemSelected android

OnOptionsItemSelected is not called in fragment xamarin android

在 OnCreate 中: SetHasOptionsMenu (true);

public override bool OnOptionsItemSelected(IMenuItem menu)
        {
            menu.SetChecked (true);
            switch (menu.ItemId) {
            case Resource.Id.selecta:
                Toast.MakeText (Application.Context, "Top", ToastLength.Long);

                return true;
            case Resource.Id.selectb:
                Toast.MakeText (Application.Context, "New", ToastLength.Long);

                return true;
            }
            return base.OnOptionsItemSelected (menu);

        }
        public override void OnCreateOptionsMenu(Android.Views.IMenu menu,MenuInflater inflater)
        {
            //MenuInflater.Inflate (Resource.Menu.Action_menu, menu);
            //menu.Clear();
            inflater.Inflate(Resource.Menu.Action_menu,menu);

            base.OnCreateOptionsMenu (menu,inflater);
        }

当复选框的项目被选中时。 OnOptionsItemSelected 未被调用。

我猜你的 Activity 中也有 OnOptionsItemSelected 的实现。

要么删除它,要么 return false 如果你想让 Fragment 有机会处理这个事件。

Activity 有机会首先处理此事件。因此,在为片段调用该方法之前,首先在 Activity 上调用 onOptionsItemSelected()。只有当它不被 Activity 处理时,片段才有机会处理它。