片段中的Android onOptionsItemSelected未触发5

时间:2016-06-07 13:36:30

标签: android android-fragments

我在android.support.v4.app.Fragment中有一个AppCompatActivity
我在片段中添加了一个自定义菜单。一切都按预期在Android 5和6中运行。但是如果我在Android 4上尝试这个,虽然我可以看到工具栏,当我点击菜单项时没有任何反应并且它不会触发onOptionsItemSelected
这是我的片段:

public class ProfileDetailFragment extends Fragment {

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

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.profile_detail_fragment, container, false);
        Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
        ((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
        ((AppCompatActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        return v;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_profile, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem menuItem) {

        switch (menuItem.getItemId()) {
            case android.R.id.home:
                getActivity().onBackPressed();
                return true;
            case R.id.tick:
                save();
                return true;
            default:
                return super.onOptionsItemSelected(menuItem);
        }
    }

    public void save(){
        Toast.makeText(getContext(), "test", Toast.LENGTH_LONG).show();
    }
}

这是我的片段布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    android:id="@+id/fragment_master"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clickable="true">
    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />
    </android.support.design.widget.AppBarLayout>
</RelativeLayout>

这是我的菜单:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/tick"
        android:title="OK"
        app:showAsAction="always"
        tools:ignore="AppCompatResource" />
</menu>

1 个答案:

答案 0 :(得分:0)

也许你的活动干扰了菜单和片段。

有两种解决方案:

*修改您的代码:确保在onOptionsItemSelected(MenuItem项目)Activity的方法中返回false