当我点击后退按钮时,我想回到我之前的片段。我在做什么不起作用。
片段替换的代码:
Fragment fragment=new MyFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in,R.anim.slide_out);
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
后退按钮的抓取代码:
@Override
public void onResume() {
super.onResume();
getActivity().invalidateOptionsMenu();
getView().setFocusableInTouchMode(true);
getView().requestFocus();
getView().setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK) {
getActivity().finish();
getActivity().overridePendingTransition(R.anim.slide_enter, R.anim.slide_exit);
return true;
}
return false;
}
});
我也试过
getActivity().getSupportFragmentManager().popBackStack();
答案 0 :(得分:0)
像这样改变这个垫子帮助
Fragment fragment=new MyFragment();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.setCustomAnimations(R.anim.slide_in,R.anim.slide_out);
fragmentTransaction.replace(R.id.container_body, fragment).addToBackStack(null);
fragmentTransaction.commit();
如果有任何错误,请告诉我
答案 1 :(得分:0)
你可以这样做。
DetailsFragment detailsFragment = new DetailsFragment();
Bundle bundle = new Bundle();
bundle.putString("ID", Id);
detailsFragment.setArguments(bundle);
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(R.id.detail_container, detailsFragment);
transaction.addToBackStack(this.getClass().getSimpleName());
transaction.hide(DetailsFragment.this);
transaction.commit();