https://kotlinlang.org logo
#compose
Title
# compose
c

Cash Hsiao

02/19/2021, 10:02 AM
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

Vitor Prado

02/19/2021, 1:08 PM
you should be using appcompat 1.3.0-beta1
see the thread above
i

Ian Lake

02/19/2021, 4:17 PM
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

Vitor Prado

02/19/2021, 4:19 PM
did you have and example of how to set ViewTreeLifecycle into decorView?
i

Ian Lake

02/19/2021, 5:03 PM
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

Vitor Prado

02/19/2021, 7:16 PM
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

Ian Lake

02/19/2021, 8:15 PM
Yep
c

Cash Hsiao

02/20/2021, 7:12 AM
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

Jeremy Woods

02/25/2021, 10:41 PM
We fixed this for DialogFragment earlier this week. You can try it out with snapshot buildId 7166224 to make sure your use case works.
5 Views