Fragment的退栈
-
在Activity中使用Fragment,我们有时可能会碰到这种需求:
- 随意切换fragment,如果是HomeFragment退出当前activity;
- 如果不是,需先返回HomeFragment,再才能退出当前activity。
这种需求很少有,但还是有方法可以实现。
下面是Android文档关于fragment退栈的示例代码:
// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack
transaction.replace(R.id.fragment_container, newFragment);
transaction.addToBackStack(null);
// Commit the transaction
transaction.commit();