当在堆栈中添加片段时,选项菜单单击不起作用。第一个片段选项项单击已完成。
Fragment Backstack的代码
public void setFragment(Fragment fragment, int title_res) {
String backStateName = fragment.getClass().getName();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = manager.beginTransaction();
if (editable_mode) {
boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
Log.e("fragmentPopped", fragmentPopped + "");
if (fragmentPopped) {
fragmentTransaction.remove(fragment);
}
fragmentTransaction.add(R.id.view_container, fragment);
fragmentTransaction.addToBackStack(backStateName);
fragmentTransaction.commit();
} else {
boolean fragmentPopped = manager.popBackStackImmediate(backStateName, 0);
Log.e("fragmentPopped", fragmentPopped + "");
if (!fragmentPopped) { //fragment not in back stack, create it.
fragmentTransaction.add(R.id.view_container, fragment);
fragmentTransaction.commit();
}
}
getSupportActionBar().setTitle(title_res);
}
答案 0 :(得分:1)
添加此行
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setHasOptionsMenu(true);
}
更新
如果从基础活动添加菜单,则将此代码添加到片段onResume()中,并从onCreate()中删除setHasOptionsMenu(true)
@Override
public void onResume() {
super.onResume();
ActivityCompat.invalidateOptionsMenu(getActivity());
}