I want to ask about an issue of DialogFragment. I ...
# compose
c
I want to ask about an issue of DialogFragment. I create a DialogFragment using ComposeView which is normally work in alpha10:
Copy code
class TestDialogFragment : DialogFragment() {
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = ComposeView(requireContext()).apply {
        setContent {
            Surface {
                TestDialogLayout()
            }
        }
    }
}
It's all normal with the deploy Preview
Copy code
@Preview
@Compose
fun PreviewTestDialog() {
    TestDialogLayout()
}
But when I navigate from other fragment like:
Copy code
TestDialogFragment().show(childFragmentManager, "Test")
or navigation way:
Copy code
findNavController().navigate(MainFragmentDirections.actionToTestDialog())
Both calls gave me this crash log:
Copy code
java.lang.IllegalStateException: ViewTreeLifecycleOwner not found from DecorView@bec849f[MainActivity]
        at androidx.compose.ui.platform.WindowRecomposerKt.createLifecycleAwareViewTreeRecomposer(WindowRecomposer.kt:231)
        at androidx.compose.ui.platform.WindowRecomposerKt.access$createLifecycleAwareViewTreeRecomposer(WindowRecomposer.kt:1)
        at androidx.compose.ui.platform.WindowRecomposerFactory$Companion$LifecycleAware$1.createRecomposer(WindowRecomposer.kt:115)
        at androidx.compose.ui.platform.WindowRecomposerPolicy.createAndInstallWindowRecomposer$ui_release(WindowRecomposer.kt:168)
        at androidx.compose.ui.platform.WindowRecomposerKt.getWindowRecomposer(WindowRecomposer.kt:216)
        at androidx.compose.ui.platform.AbstractComposeView.ensureCompositionCreated(ComposeView.kt:184)
        at androidx.compose.ui.platform.AbstractComposeView.onAttachedToWindow(ComposeView.kt:215)
        at android.view.View.dispatchAttachedToWindow(View.java:19553)
I'm not sure what happened during the alpha10 to alpha12... anyone has idea?
v
you should be using appcompat 1.3.0-beta1
see the thread above
i
Dialogs (and specifically dialogs owned by DialogFragments) don't have the ViewTree APIs called on their decorView so you'd need to do that yourself. Do you mind filing an issue against Fragments so that we can do that for you? https://issuetracker.google.com/issues/new?component=460964&template=1182267
👍 1
v
did you have and example of how to set ViewTreeLifecycle into decorView?
i
ViewTreeLifecycleOwner.set(dialog.decorView, lifecycleOwner)
🙌 2
Not sure when is the right time to call that though, I'd probably first try an
OnShowListener
v
the
decorView
is my rootview?
this works for me:
Copy code
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    dialog?.window?.decorView?.let { 
        ViewTreeLifecycleOwner.set(it, viewLifecycleOwner)
    }
}
👍 1
i
Yep
c
I've create the issue https://issuetracker.google.com/issues/180691023 And setting ViewTreeLifecycleOwner at the beginning of onCreateView works fine! Thanks! (BTW setting in
OnShowListener
is too late)
j
We fixed this for DialogFragment earlier this week. You can try it out with snapshot buildId 7166224 to make sure your use case works.