简体   繁体   English

带有保留片段的Android导航组件并输入动画

[英]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.

相关问题 在android导航组件中添加进入动画以开始目的地 - Add enter animation to start destination in android navigation component 反向进入和退出 animation 用于 RTL 布局 - Android 导航组件 - Reverse enter and exit animation for RTL layout - Android Navigation Component 片段未显示在 Android 导航组件中 - Fragments are not displaying in Android Navigation component 弹出输入 animation 不适用于 Jetpack 导航组件 - Pop enter animation not working with the Jetpack Navigation Component Android 导航组件:在片段中传递值(参数) - Android Navigation Component : Pass value (arguments) in fragments Android 导航组件不适用于对话框片段 - Android Navigation Component not working with Dialog Fragments 如何使用导航组件向不断变化的片段添加动画? - How to add animation to changing fragments using Navigation Component? 保留的片段未在Android活动中以保存的实例状态保留 - Retained fragments not retained with saved instance state in Android activity 使用 Android 导航组件在底部导航中的多个片段之间共享视图模型 - Share viewmodel between multiple fragments in Bottom Navigation with Android Navigation Component 如何通过 android 中的抽屉在导航中的片段之间进行 animation 过渡 - how to make animation transition between fragments in navigation by drawer in android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM