question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Bug][Feature request][Crash] - Compose support

See original GitHub issue

Hi @B3nedikt For now ViewPump is not working for fragments created like this:

class ComposeFragment : Fragment() {
   
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return ComposeView(requireContext()).apply {
            setContent {
                MxdTheme {
                    setUpComposable()
                }
            }
        }
    }

    /**
     * Should be implemented by child views to set up [Composable].
     */
    @Composable
    abstract fun setUpComposable()
}

ViewPumpAppCompatDelegate.onCreateView methods are not even called. If setUpComposable() contains AndroidView with WebView inside, app will crash due to wrong resources passed to the it.

@Composable
fun setUpComposable() {
    Box {
        AndroidView(
            factory = {
                var context = it

                // Here we are getting modified context and resources
                Timber.d("webViewFactory: context=$context")
                Timber.d("webViewFactory: resources=${context.resources}")

                webViewFactory(
                    context = context,
                    onRedirectAttempt = onRedirectAttempt,
                    onRedirectStarted = onRedirectStarted,
                    onRedirectProgressChanged = onRedirectProgress,
                    onRedirectFinished = onRedirectFinished
                )
            },
            update = {
                webViewUpdate(
                    webView = it,
                    userAgent = userAgent,
                    url = url
                )
            }
        )

        if (progress != 1.0f) {
            LinearProgressIndicator(
                modifier = Modifier
                    .height(2.dp)
                    .fillMaxWidth(),
                progress = progress,
                color = Color(0xFFE20019)
            )
        }
    }
}

/**
 * The factory block will be called exactly once to obtain the View to be composed, and it is also
 * guaranteed to be invoked on the UI thread. Therefore, in addition to creating the factory, the
 * block can also be used to perform one-off initializations and View constant properties' setting.
 */
private fun webViewFactory(
    context: Context,
    onRedirectAttempt: (url: String) -> Boolean,
    onRedirectStarted: (url: String) -> Unit,
    onRedirectProgressChanged: (progress: Int) -> Unit,
    onRedirectFinished: (url: String) -> Unit
): WebView {
    return WebView(context).apply {
        settings.javaScriptEnabled = true
        webViewClient = object : WebViewClient()
        webChromeClient = object : WebChromeClient()
    }
}

/**
 * The update block can be run multiple times (on the UI thread as well) due to recomposition,
 * and it is the right place to set View properties depending on state. When state changes,
 * the block will be reexecuted to set the new properties. Note the block will also be ran once
 * right after the factory block completes.
 */
private fun webViewUpdate(
    webView: WebView,
    userAgent: String,
    url: String
) {
    webView.setUserAgent(userAgent)
    webView.loadUrl(url)
}

Do you have any plans to add Compose support? If not, could you please at least give some advises on how to fix this?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:9 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
iTruffcommented, Nov 6, 2021

@B3nedikt Wow, that was fast 😄 Cool when you know a lot about Context in Android 😃 Just checked your solution, works like a charm, thanks a lot. For now I’ll keep it as is, will keep you posted in case of better solution.

0reactions
B3nediktcommented, Nov 7, 2021

Good to know, feel free to reopen when you find a better solution 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Maine Crash Reporting Online Search & Ordering Service
Search and request crash reports online from the Maine State Police Crash Database. You can search by name, date of birth, crash location,...
Read more >
Request a crash report | Mass.gov
If you need the official police report for a vehicle crash, you can get a copy from the Registry of Motor Vehicles.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found