android - 片段被绘制在彼此之上
问题描述
场景是这样的......我单击一个加载下一个片段的按钮。虽然动画仍在播放,但我点击了最近的应用程序按钮。显示最近的应用程序屏幕后,我返回我的应用程序。
然后我看到两个片段相互重叠。好像Android忘记删除以前的片段。我单击返回以删除所有片段,直到到达此 FragmentActivity 中加载的第一个片段。我记录了从 FragmentManager 获得的所有片段,它只显示一个。但我仍然看到两个片段一个一个叠加。应该删除的一个没有响应触摸事件,而另一个则按应有的方式响应。
这是Android错误还是我的错?有没有办法解决这个问题或防止它发生?
它不会每次都发生。这就是我加载片段的方式
public void loadNewFragment(AnimFragment newFragment, boolean addToSTack, boolean animate, String tag) {
if (newFragment != null) {
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.setCustomAnimations(R.anim.fragment_enter_from_right, R.anim.fragment_exit_to_left, R.anim.fragment_enter_from_left, R.anim.fragment_exit_to_right);
transaction.replace(R.id.fragment_container, newFragment, tag);
if (addToSTack) {
transaction.addToBackStack(tag);
}
int transactionId = transaction.commit();
newFragment.setTransactionId(transactionId);
}
}
然后我用背压将它们移除
getSupportFragmentManager().popBackStackImmediate();
解决方案
在我看来,“下方”片段是onPause()
状态,它仍然存在且可见。
尝试在前端片段的根布局上使用白色背景,以避免显示下面的片段。此外,前端片段的根布局大小(宽度和高度)应为 match_parent。
检查此站点以了解Fragment
生命周期。
推荐阅读
- c - 指针/结构 free() - 堆损坏
- python - 在 Python 中的 dict 内的列表中搜索 - Pymongo
- javascript - 从构造函数中确定 tagName
- nginx - Nginx 代理通行证不渲染
- r - 如何根据不同的类找到多少个唯一值
- spring-boot - Spring Cloud Contract:在运行时找不到本地合约生成存根
- java - HQL查询左连接和带有数组的bean
- javascript - Laravel $request->all() 没有返回任何响应 ajax 请求
- javascript - 如何在javascript中加载没有点击事件的函数
- python - 为什么 Pipenv 没有选择我的 Pyenv 版本?