替换 Android 中的片段的奇怪行为
Strange behaviour replacing fragments in Android
我已经在一个应用程序中工作了几个月。上传到 Playstore 的版本工作正常。最近,做了一些改进,整个应用程序开始以一种非常奇怪的方式工作。
如果你在片段之间切换,一段时间后它不会正确地膨胀片段,总是放第一个的内容。因此,如果您切换片段,操作栏会发生变化,但内容始终相同(并且您无法与之交互)。
这是错误的快速视频 https://streamable.com/9exa
我要疯了,因为没有任何日志或跟踪。
我使用的是常用的切换片段的方式,最有趣的部分是一开始一切正常:
Fragment fragment = getSelectedFragment(index); // returns the desired fragment
FragmentManager fragmentManager = getFragmentManager();
//Pop backstack
for(int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
有人知道这里会发生什么吗?谢谢!
编辑:我也曾尝试清除后台堆栈以防出现问题,但它并没有改变任何东西
已解决:正如我在回答中所解释的那样,我发现这是一个与 SwipeRefreshLayout 相关的问题。有关此问题的更多信息,请访问 When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work
你用的是什么片段?
我在使用框架片段时遇到了一些奇怪的行为,我切换到 V4 后一切正常
请检查此 link:FragmentManager popBackStack doesn't remove fragment
我在 activity 中使用以下代码在片段之间切换,它工作正常。我的应用的 minSdkVersion 是 15.
private void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
}
好的,我发现问题了。
似乎 SwipeRefreshLayout 是造成所有问题的原因。我的一个片段正在使用它,在切换片段几次后它卡住了。
删除它,或禁用它 setEnabled(false) 解决所有问题。
希望对遇到同样问题的人有所帮助!
SO 中提到相同问题的另一个线程:When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work
我已经在一个应用程序中工作了几个月。上传到 Playstore 的版本工作正常。最近,做了一些改进,整个应用程序开始以一种非常奇怪的方式工作。
如果你在片段之间切换,一段时间后它不会正确地膨胀片段,总是放第一个的内容。因此,如果您切换片段,操作栏会发生变化,但内容始终相同(并且您无法与之交互)。
这是错误的快速视频 https://streamable.com/9exa
我要疯了,因为没有任何日志或跟踪。
我使用的是常用的切换片段的方式,最有趣的部分是一开始一切正常:
Fragment fragment = getSelectedFragment(index); // returns the desired fragment
FragmentManager fragmentManager = getFragmentManager();
//Pop backstack
for(int i = 0; i < fragmentManager.getBackStackEntryCount(); ++i) {
fragmentManager.popBackStack();
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
有人知道这里会发生什么吗?谢谢!
编辑:我也曾尝试清除后台堆栈以防出现问题,但它并没有改变任何东西
已解决:正如我在回答中所解释的那样,我发现这是一个与 SwipeRefreshLayout 相关的问题。有关此问题的更多信息,请访问 When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work
你用的是什么片段?
我在使用框架片段时遇到了一些奇怪的行为,我切换到 V4 后一切正常
请检查此 link:FragmentManager popBackStack doesn't remove fragment
我在 activity 中使用以下代码在片段之间切换,它工作正常。我的应用的 minSdkVersion 是 15.
private void setFragment(Fragment fragment) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container, fragment);
fragmentTransaction.commit();
}
好的,我发现问题了。
似乎 SwipeRefreshLayout 是造成所有问题的原因。我的一个片段正在使用它,在切换片段几次后它卡住了。
删除它,或禁用它 setEnabled(false) 解决所有问题。
希望对遇到同样问题的人有所帮助!
SO 中提到相同问题的另一个线程:When switch fragment with SwipeRefreshLayout during refreshing, fragment freezes but actually still work