仅膨胀特定的操作栏菜单

Inflating only a specific action bar menu

我在主 activity 中定义了一个操作栏菜单。现在,我在 activity 中使用的片段之一有其自己的操作栏菜单,但是当我单击菜单选项时,我同时获得了片段和 activity 的菜单项。如何确保只显示该片段的菜单项?

我对该片段的 java 代码是:

public class ProfileD extends Fragment {

TextView tv_named, tv_genderd, tv_cfd, tv_aged, tv_biod, tv_statusd;
ImageView imageView_dp;

public ProfileD() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_profile_d, container, false);

    tv_named = (TextView)rootView.findViewById(R.id.tv_named);
    tv_genderd = (TextView)rootView.findViewById(R.id.tv_genderd);
    tv_cfd= (TextView)rootView.findViewById(R.id.tv_cfd);
    tv_aged = (TextView)rootView.findViewById(R.id.tv_aged);
    tv_biod = (TextView)rootView.findViewById(R.id.tv_biod);

    tv_statusd = (TextView) rootView.findViewById(R.id.tv_statusd);

    imageView_dp = (ImageView)rootView.findViewById(R.id.imageView_dp);
    setHasOptionsMenu(true);

    return rootView;
}

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

    super.onCreateOptionsMenu(menu, inflater);

    getActivity().getMenuInflater().inflate(R.menu.profile_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.edit_profile) {

        Intent intent = new Intent(getContext(),PersonalDetails.class);
        intent.putExtra("Edit",1);
        startActivity(intent);

        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onResume() {
    super.onResume();

    AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity();
    ActionBar actionBar = appCompatActivity.getSupportActionBar();
    actionBar.setTitle("Profile");
}
}

菜单的 xml 文件是:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/edit_profile"
    android:orderInCategory="100"
    android:title="Edit"
    app:showAsAction="never" />

</menu>

我找到了解决问题的办法。我将它张贴在这里,如果其他人面临同样的问题,他们可以从中受益:

我创建了一个静态菜单对象并将 activity 的操作栏菜单存储在该对象中。然后我从片段中调用该对象并清除它,从而清除 activity 的菜单并留下片段的菜单

这是更新后的java片段代码

public class ProfileD extends Fragment {

TextView tv_named, tv_genderd, tv_cfd, tv_aged, tv_biod, tv_statusd;
ImageView imageView_dp;

public ProfileD() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_profile_d, container, false);

    tv_named = (TextView)rootView.findViewById(R.id.tv_named);
    tv_genderd = (TextView)rootView.findViewById(R.id.tv_genderd);
    tv_cfd= (TextView)rootView.findViewById(R.id.tv_cfd);
    tv_aged = (TextView)rootView.findViewById(R.id.tv_aged);
    tv_biod = (TextView)rootView.findViewById(R.id.tv_biod);

    tv_statusd = (TextView) rootView.findViewById(R.id.tv_statusd);

    imageView_dp = (ImageView)rootView.findViewById(R.id.imageView_dp);
    setHasOptionsMenu(true);

    return rootView;
}

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

    super.onCreateOptionsMenu(menu, inflater);

    HomePage.menu_home.clear();

    getActivity().getMenuInflater().inflate(R.menu.profile_menu, menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.edit_profile) {

        Intent intent = new Intent(getContext(),PersonalDetails.class);
        intent.putExtra("Edit",1);
        startActivity(intent);

        return true;
    }

    return super.onOptionsItemSelected(item);
}

@Override
public void onResume() {
    super.onResume();

    AppCompatActivity appCompatActivity = (AppCompatActivity)getActivity();
    ActionBar actionBar = appCompatActivity.getSupportActionBar();
    actionBar.setTitle("Profile");
}
}