对以下代码有什么想法吗? 在我的测试中,我发现替换的片段没有被破坏,并且在弹出后堆栈时实例仍然存在。 只是想验证这是使用片段事务的有效方法。
getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(frame, fragmentB).commit();
我使用 replace 的原因是它会导致被替换的片段运行其退出动画。
你可以参考 android 设计师指南进行片段交易:http://developer.android.com/guide/components/fragments.html
具体来说,是下面的代码片段:
// 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();
所以是的,您正在做的是替换片段的正确方法。