首页 > 解决方案 > 如何防止破坏碎片

问题描述

在我的活动中,我通过以下代码加载 4 个片段:

 supportFragmentManager.beginTransaction()
                    .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                    .replace(R.id.frame, fragmentList[0])
                    .commit()

但问题是每个片段都有大量数据,应该从服务器加载大量数据。这需要一点点。我希望我的片段在我更改它们以防止再次加载数据时不会被破坏。我怎样才能做到这一点?有什么办法可以隐藏碎片而不是破坏,通过加载,它们就会出现?

标签: androidandroid-fragmentskotlin

解决方案


而不是替换使用.add(containerId, newFragment)然后.hide(currentFragment)

编辑:

supportFragmentManager.beginTransaction()
                        .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                        .add(R.id.frame, fragmentList[0])
                        .hide(fragmentList[1]) // assumed that fragmentList[1] is your current fragment
                        .commit()

推荐阅读