Android - when exiting a WebView, the audio still keeps playing

Accepted answer didnt work for me - just using onPause in a fragment was not enough to stop music on Android 9. I had to also do:

override fun onDestroyView() {
    webview?.destroy()

    super.onDestroyView()
}

You should call through to the WebView's onPause() and onResume() from your Activity's onPause() and onResume(), respectively.

Pauses any extra processing associated with this WebView and its associated DOM, plugins, JavaScript etc. For example, if this WebView is taken offscreen, this could be called to reduce unnecessary CPU or network traffic. When this WebView is again "active", call onResume().


There's also pauseTimers(), which affects all of the WebViews within your application:

Pauses all layout, parsing, and JavaScript timers for all WebViews. This is a global requests, not restricted to just this WebView. This can be useful if the application has been paused.


Same issue for the Jetpack Compose can be solved by capturing the WebView (https://google.github.io/accompanist/webview/) in "onCreated" and then use it to call "destroy()"

val webViewState = rememberWebViewState(webViewUrl.value)
val webView = remember { mutableStateOf<android.webkit.WebView?>(null) }

//..inside Composable..
WebView(state = webViewState,
  captureBackPresses = true,
  onCreated = { wv ->
    webView.value = wv
  }
)

//..on Back Press/Close Button..
webView.value?.destroy()