Skip to content

Commit

Permalink
Add onDispose param
Browse files Browse the repository at this point in the history
Destroy webview by default on dispose
  • Loading branch information
rlatapy-luna committed Jun 30, 2022
1 parent 0fa796a commit 374bdf3
Showing 1 changed file with 13 additions and 0 deletions.
13 changes: 13 additions & 0 deletions web/src/main/java/com/google/accompanist/web/WebView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import android.webkit.WebView
import android.webkit.WebViewClient
import androidx.activity.compose.BackHandler
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.Immutable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.Stable
Expand All @@ -33,10 +34,13 @@ import androidx.compose.runtime.mutableStateListOf
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.rememberUpdatedState
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalInspectionMode
import androidx.compose.ui.viewinterop.AndroidView
import com.google.accompanist.web.LoadingState.Finished
import com.google.accompanist.web.LoadingState.Loading
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.MutableSharedFlow
Expand Down Expand Up @@ -68,6 +72,7 @@ fun WebView(
captureBackPresses: Boolean = true,
navigator: WebViewNavigator = rememberWebViewNavigator(),
onCreated: (WebView) -> Unit = {},
onDispose: (WebView) -> Unit = { it.destroy() },
client: AccompanistWebViewClient = remember { AccompanistWebViewClient() },
chromeClient: AccompanistWebChromeClient = remember { AccompanistWebChromeClient() }
) {
Expand All @@ -81,6 +86,14 @@ fun WebView(
with(navigator) { webView?.handleNavigationEvents() }
}

val currentOnDispose by rememberUpdatedState(onDispose)

webView?.let { it ->
DisposableEffect(it) {
onDispose { currentOnDispose(it) }
}
}

// Set the state of the client and chrome client
// This is done internally to ensure they always are the same instance as the
// parent Web composable
Expand Down

0 comments on commit 374bdf3

Please sign in to comment.