三星设备对待FragmentTransactions的方式与其他设备不同吗?

时间:2015-07-27 19:19:05

标签: android android-fragments fragmenttransaction samsung-touchwiz

我一直在努力解决一个让我疯狂多年的问题。我有一个应用程序,其中一个活动通过动态片段交换显示不同的屏幕,即通常的

getFragmentManager().beginTransaction().replace(R.id.fragementContainer, new SomeFragment()).commit();

这适用于我的Nexus 4,Nexus 5,Nexus 7 2012,Nexus 7 2013,Moto G 1st gen等。除三星设备(SGS5,SGS6)外,其他大部分都是如此。在这些设备上,有时(很少但很少见) replace()调用似乎被误解为 add()调用,新片段出现在前一个。

我试过复制这个bug,但似乎没有任何模式。由于我在任何地方都没有收到任何错误消息,因此我无法对其进行故障排除。

2 个答案:

答案 0 :(得分:3)

这就是我总是使用支持库的原因。源代码在您的项目中,并未被第三方修改。你已经在使用支持包吗?

答案 1 :(得分:1)

这很可能是由一些三星“优化”造成的。我唯一可以建议的是使用remove()add()代替replace()FragmentTransaction内的实现是不同的,因此这可能会有不同的行为。

您实际上可以轻松删除该片段,因为它与容器具有相同的ID。试试这个:

FragmentTransaction transaction = fragmentManager.beginTransaction();

Fragment oldFragment = fragmentManager.findFragmentById(R.id.container);
if (oldFragment != null) transaction.remove(oldFragment);

transaction.add(R.id.container, new Fragment(), "tag").commit();