0

I am creating ComposeView in fragment using ComposeView like so:

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
    return ComposeView(requireContext()).apply {
        setContent {
            var hideKeyboard by remember { mutableStateOf(false) }
            Box(modifier = Modifier
                    .fillMaxSize()
                    .clickable {
                        hideKeyboard = true
                    }) {
                val backgroundDrawable =
                        viewModel.onViewCreatedStateFlow.collectAsState().value
                if(backgroundDrawable != null) {
                    Image(
                            modifier = Modifier.fillMaxSize(),
                            contentDescription = "",
                            painter = painterResource(tv.pluto.library.resources.R.attr.drawableWindowBackground)
                            )
                }...
}}

I get a Resource Not Found exception. As you've noticed, the current if-statement uses a backgroundDrawable that is emitted when onViewCreated is called. When I pass that value to painter instead, the code works as expected. Why am I not able to access the resource in the composable at the point when the event occurs? How am I supposed to make this work?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Regress.arg
  • 352
  • 6
  • 16

0 Answers0