https://kotlinlang.org logo
#compose-android
Title
# compose-android
c

Colton Idle

03/07/2024, 6:08 AM
Need some help with an interesting dilema I have. I need to show a webview while saving its state (this is because the webview is 1 tab out of 4 for my app and so when user goes to tab 2-4, then back to 1, I want the webview to still have it's contents). I followed the sample here https://github.com/google/accompanist/blob/main/sample/src/main/java/com/google/accompanist/sample/webview/WebViewSaveStateSample.kt#L84-L90
Copy code
LaunchedEffect(navigator) {
        val bundle = webViewState.viewState
        if (bundle == null) {
            // This is the first time load, so load the home page.
            navigator.loadUrl("<https://bbc.com>")
        }
    }
Easy! But now I need to add an auth header. Okay not bad
Copy code
navigator.loadUrl("<https://bbc.com>", "AUTH" to $token)
but now my issue is that when the token is updated, it doesn't cause a reload. How would you solve this?
So basically my current code is
Copy code
LaunchedEffect(navigator) {
        val bundle = webViewState.viewState
        if (bundle == null) {
            // This is the first time load, so load the home page.
            navigator.loadUrl("<https://bbc.com>", "AUTH" to $token)
        }
    }
but not sure how to best update this. If I try to add a key of token to the launchedEffect it of course doesn't get called because the bundle isn't null. It's almost as if it'd be helpful if I could look into the current webview and see if there is already a header applied, but it doesn't seem possible to get request headers from a webview instance.
m

MR3Y

03/07/2024, 7:40 PM
What about ?
Copy code
var isFirstTime by rememberSaveable(webViewState) { mutableStateOf(true) }
....
// Inside bundle == null check
isFirstTime = false
// Add separate LaunchedEffect block keyed with token
LaunchedEffect(token) {
    if(!isFirstTime) {
        navigator.loadUrl(url, token)
    }
}
More or less something like this. I'm on mobile so code formatting might look weird :).
c

Colton Idle

03/07/2024, 8:08 PM
Let me give that a shot
z

zt

03/11/2024, 1:26 AM
c

Colton Idle

03/11/2024, 3:20 AM
I have not. Let me see if they have something novel there. either way TIL of a KMP webview. nice!
3 Views