我希望能够通过使用addToBackStack()来反转替换FragmentTransaction:
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction(); Fragment scheduleFragment = new ScheduleFragment(); fragmentTransaction.replace(R.id.content_container,scheduleFragment,"scheduleFragment"); fragmentTransaction.addToBackStack("scheduleFragment"); fragmentTransaction.commit();
但在此之后,单击后退按钮不会执行任何操作.
从文档和它应该扭转交易.
我错过了什么?
解决方法
正确的方法是使用onBackPressed()方法捕获应用程序中的返回事件,然后使用popBackStack()“弹出”backStack.例如:
public void onBackPressed() { // Catch back action and pops from backstack // (if you called prevIoUsly to addToBackStack() in your transaction) if (getSupportFragmentManager().getBackStackEntryCount() > 0){ getSupportFragmentManager().popBackStack(); } // Default action on back pressed else super.onBackPressed(); }
PD:很抱歉延迟回答,但我刚看到你的问题.希望能帮助到你!