是否可以在片段事务中添加ToBackStack并替换?

bgo*_*son 17 android android-fragments fragmenttransaction

有关以下代码的任何想法?在我的测试中,我发现被替换的片段没有被破坏,当弹出后栈时实例仍然存在.只是想验证这是使用片段事务的有效方法.

getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(frame, fragmentB).commit();
Run Code Online (Sandbox Code Playgroud)

我使用替换的原因是它导致替换的片段运行它的退出动画.

Jas*_* Hu 24

你可以参考片段交易的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();
Run Code Online (Sandbox Code Playgroud)

所以,是的,你正在做的是更换碎片的正确方法.