我已经在片段中创建了一个自定义菜单,它工作正常,但问题是当我回去时,该菜单显示在所有活动和片段中,我不希望它显示在所有,所以帮助我。
public class DriverLogDetialsFragment extends Fragment {
private MainActivity mainActivity;
public DriverLogDetialsFragment() {
// Required empty public constructor
}
public static DriverLogDetialsFragment newInstance(MainActivity mainActivity) {
DriverLogDetialsFragment fragment = new DriverLogDetialsFragment();
fragment.mainActivity = mainActivity;
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
//setHasOptionsMenu(true);
return inflater.inflate(R.layout.fragment_driverlog_detials, container, false);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_log_detials, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_email) {
View view = LayoutInflater.from(getActivity()).inflate(R.layout.dialog_email, null);
AlertDialog.Builder bulider = new AlertDialog.Builder(getContext());
bulider.setView(view);
bulider.setCancelable(false);
bulider.setPositiveButton("Send", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
bulider.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
bulider.show();
}
return super.onOptionsItemSelected(item);
}
}