1

The variable singleArticle.content I used in the AndroidView is returning an empty value. But if I display it outside AndroidView the value appear correctly. Can someone know please what's the issue with my code

@Composable
fun ArticleDetailScreen(
    navController: NavController,
    viewModel: ArticleDetailViewModel = hiltViewModel()
) {
    ...
    CustomWebView(viewModel.article.value)
    ...
}

@Composable
fun CustomWebView(singleArticle: Article) {
    AndroidView(
        factory = { context ->
            WebView(context).apply {
                loadData(singleArticle.content, "text/html", "UTF-8")
            }
        },
    )
}
0

1 Answer 1

4

A possible reason is that your singleArticle.content initially empty, and gets updated after view appears.

AndroidView factory is getting called only once when the view is created. To sync it with Compose state updates, you should use update block:

AndroidView(
    factory = { context ->
        WebView(context)
    },
    update = {
        it.loadData(singleArticle.content, "text/html", "UTF-8")
    },
)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.