https://kotlinlang.org logo
#compose
Title
# compose
k

Konyaco

09/10/2020, 1:45 AM
I added a WebView to my app by using AndroidView compose function, it throws
java.lang.IllegalStateException: KeyEvent can't be processed because this key input node is not active.
when I press any key (including backpress, hardware keyboard keys). Do you have the same problem? How can I fix it? 🤔thanks
👍 1
a

Ali Zargar Shabestari

09/10/2020, 5:21 AM
I used WebView in my compose code successfully. Can you please share your code?
k

Konyaco

09/10/2020, 5:30 AM
Copy code
class CommunityActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContent {
            MyTheme {
                Surface(
                    modifier = Modifier.fillMaxSize(),
                    color = MaterialTheme.colors.background
                ) {
                    Community(onClose = ::close)
                }
            }
        }
    }

    private fun close() {
        finish()
    }
}

@OptIn(ExperimentalKeyInput::class)
@Composable
fun Community(onClose: () -> Unit) {
    val backgroundColor = MaterialTheme.colors.background
    var loading by remember { mutableStateOf(true) }
    Column(Modifier.fillMaxSize()) {
        TopAppBar(title = {
            Text("Title")
        }, navigationIcon = {
            IconButton(onClick = onClose) {
                Icon(Icons.Filled.Close)
            }
        }, backgroundColor = MaterialTheme.colors.surface, actions = {
            Crossfade(current = loading) {
                if (it) CircularProgressIndicator()
            }
        })
        AndroidView(
            modifier = Modifier.fillMaxSize(),
            viewBlock = {
                WebView(it).apply {
                    settings.apply {
                        javaScriptEnabled = true
                        setSupportZoom(false)
                        javaScriptCanOpenWindowsAutomatically = true
                        domStorageEnabled = true
                        setBackgroundColor(backgroundColor.value.toInt())
                    }
                    webChromeClient = WebChromeClient()
                    webViewClient = object : WebViewClient() {
                        override fun onPageStarted(view: WebView?, url: String?, favicon: Bitmap?) {
                            loading = true
                        }

                        override fun onPageFinished(view: WebView?, url: String?) {
                            loading = false
                        }
                    }
                    loadUrl("<https://www.google.com>")
                }
            },
            update = {}
        )
    }
}
a

Ali Zargar Shabestari

09/10/2020, 6:02 AM
I played with you code a bit. it seems like the problem comes from
update = {}
. removing it solved the issue! don’t ask me why. I only replaced my code with yours and commented out the sections one by one :D
k

Konyaco

09/10/2020, 11:03 AM
The default parameter
NoOpUpdate
is just an alias of
{}
. It seems to be caused by other factors.
m

Madhava

09/28/2020, 6:15 AM
@Konyaco, I am having this exact problem! Did you solve it? Its really annoying. Once the WebView gets the keyboard focus, it seems to break the back button and sometimes it causes the app to crash or other times i get back to the previous screen but i cant touch anything anymore.
k

Konyaco

09/28/2020, 8:16 AM
@Madhava Not yet, and I found that the problem not only occurs in WebView but also in every Android View witch uses keyboard input. (Such as EditView). Current version is alpha-03.
m

Madhava

09/28/2020, 11:00 AM
Yes, it’s frustrating. I didn’t see this issue with dev15/16, also I have web views for some text and it’s generally fine even though occasionally you can select the text it never causes crashes.
I think it’s to do with the HTML inputs and the keyboard
I have a web view with js and a form and as long as I never engage with the inputs it never breaks
Mine crashes on a real device too! Perhaps I should file a bug. 🐛
a

allan.conda

09/30/2020, 1:20 AM
Just star the issue above I think. It’s mentioning emulator but you could mention it failing for real device as well (pressing navigation bar buttons)
13 Views