當我單擊後退按鈕時,我想要移回到之前的片段。我正在做的是不工作。如何在Android中返回前一個片段
的片段替換代碼:
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();
fragmentTransaction.addtobackstack(fragment.getClass()。getSimpleName()); –
我已經執行但仍然沒有工作 – Shivangi
看到這個鏈接http://stackoverflow.com/questions/18305945/how-to-resume-fragment-from-backstack-if-exists –