[英]Android Navigation Component with Retained Fragments and enter animation
I am trying to use the Navigation Component with a retained Fragment and an enter animation. 我正在尝试将导航组件与保留的Fragment和Enter动画一起使用。 The fragment appears with the animation as expected.
该片段将与预期的动画一起出现。 On rotating my device I get the following crash:
旋转设备时,出现以下崩溃:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
I can solve this by removing the enter animation (from the nav graph) or by making the fragment not be retained. 我可以通过删除输入动画(从导航图)或不保留片段来解决此问题。
Does anyone have a clue how to solves this as I wanted both retained and the animation. 有谁知道如何解决这个问题,因为我希望保留动画。
Here is my onCreate: 这是我的onCreate:
private var binding: FragmentDemoBinding? = null
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? {
if (binding == null) {
binding = FragmentDemoBinding.inflate(inflater, container, false)
}
retainInstance = true
return binding?.root
}
edit : Actually on reading a bit more I am not sure it makes sense to try and use retained fragments within the navigation component. 编辑 :实际上,在阅读更多内容时,我不确定尝试在导航组件中使用保留的片段是否有意义。 If I need to persist data on configuration changes I guess a headless fragment or use onSaveInstance or ViewModel component etc.
如果我需要在配置更改时保留数据,我猜一个无头的片段或使用onSaveInstance或ViewModel组件等。
In onDestroyView method remove all views or just animation view. 在onDestroyView方法中,删除所有视图或仅删除动画视图。
override fun onDestroyView() {
if(view != null){
val parent = view.parent
parent.removeAllViews
}
super.onDestroyView()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.