[Bug][Feature request][Crash] - Compose support
See original GitHub issueHi @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:
- Created 2 years ago
- Comments:9 (4 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@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.Good to know, feel free to reopen when you find a better solution 😃