Hi, All. I'm using Webview in compose. This Webvie...
# compose
b
Hi, All. I'm using Webview in compose. This Webview is used in Activity and internal Composable, so I'm trying to create a LocalWebview with CompositionLocalOf and implement the same object in many nested Composable and Activity, is there a better way?
it is sample code
Copy code
val LocalWebView = staticCompositionLocalOf<WebView?> { null }

// in activity
CompositionLocalProvider(
  LocalWebView provides WebView(context)
) { ... }

// use internal composable 
val webView = LocalWebView.current
AndroidView(factory = { webView... }, ...)
a
what are u trying to achieve? why do u want to forward the webview to ur ui tree?
b
share the WebView in a nested composable and activity. I need to control the same WebView from multiple pages
a
> share the WebView in a nested composable and activity this is not what you are trying to achieve. this is 'how' you are trying to achieve it. I am asking this in terms of your app's functionality. > I need to control the same WebView from multiple pages no such thing as a 'page' in compose, so I don't get what you mean by this. are u building a composable that you want to be displaying web content and its contents need to be able to control that web content? if yes, it would be better to have some sort of a mutable state which you pass down your content instead, and use that in your AndroidView's update to do the wiring
b
Sorry my explanation was insufficient. I am currently divided into several feature modules, and I am trying to use the same webview in these screens(composable). It has a structure with one Activity, and nested screens(composable) are displayed by using NavHost. In this situation, when trying to hand over the webview as a parameter to the nested composable, there is a problem that too much composable has to go through.
Copy code
// in activity
onCreate(...) {
  setContent {
    MainNavHost { // custom navhost
      composable(...) { AScreen(webView) } 
      // AScreen in A module
      
      composable(...) { BScreen(webView) } 
      // BScreen in B module
      ...
    }
  }
}

// using other function in activity
onPause(...) { webView.onPause() }
a
why do you need to share the exact same webview across different screens?
b
because... 1. Recomposition occurs when navigating from B to C(nested composable) and then returning to B(of course) 2. It is efficient to synchronize events or states on multiple screens using one web view.
a
1. so? 2. what does efficient mean? are u optimizing for performance?
b
• The WebView is redrawn due to the result of 1. This means that when the nested screen(Composable) is accumulated, the WebView is continuously redrawn unnecessarily if go back to the previous screen(Composable). • When creating and using WebView on each of several screens, it becomes difficult to share events or states with previous screens. In addition, when creating each WebView, many overlapping codes occur because options such as settings must be created every time.