gpt4 book ai didi

android - 在 fragment 事务中添加和替换是否可以?

转载 作者:IT老高 更新时间:2023-10-28 23:21:58 27 4
gpt4 key购买 nike

对以下代码有什么想法吗?在我的测试中,我发现替换的 fragment 没有被破坏,并且在弹出返回堆栈时实例仍然存在。只是想验证这是使用 fragment 事务的有效方式。

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

我使用替换的原因是它会导致被替换的 fragment 运行它的退出动画。

最佳答案

fragment事务可以引用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();

所以是的,您正在做的是替换 fragment 的正确方法。

关于android - 在 fragment 事务中添加和替换是否可以?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19846233/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com