可以在片段事务中添加toToBackStack并进行替换吗? [英] Is it OK to addToBackStack and replace in a fragment transaction?

查看:68
本文介绍了可以在片段事务中添加toToBackStack并进行替换吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对以下代码有何想法?在我的测试中,我发现替换后的片段没有被破坏,并且在弹出后退堆栈时该实例仍然存在.只是想确认这是使用片段事务的有效方法.

Any thoughts on the following code? In my testing I've found the replaced fragment isn't destroyed and the instance is still around when popping the back stack. Just looking to verify that this is a valid way to use fragment transactions.

getSupportFragmentManager().beginTransaction().addToBackStack(null).replace(frame, fragmentB).commit();

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

My reason for using replace is that it causes the replaced fragment to run it's exit animation.

推荐答案

您可以参考android设计器指南中的片段事务: http://developer.android.com/guide/components/fragments.html

You can refer to the android designer guide for fragment transaction: 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();

是的,您正在做的是替换片段的正确方法.

So yes, what you are doing is the correct approach in replacing fragments.

这篇关于可以在片段事务中添加toToBackStack并进行替换吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆