2

I want show video in WebView, and for this i want use this code :

    WebSettings settings = post_content_web.getSettings();
    post_content_web.setWebChromeClient(new WebChromeClient());
    settings.setPluginState(WebSettings.PluginState.ON);
    settings.setJavaScriptEnabled(true);

    settings.setDefaultTextEncodingName("utf-8");

    post_content_web.loadDataWithBaseURL("", myCustomStyleString + "<div style=\"direction:rtl\">"
            + title + "\n\n" + content + "</div>", "text/html", "utf-8", null);

With above codes i can show video but when video is playing and when click on BackPressButton not stop this video and video play!
I want when click on BackButton stop this video!

How can i it?

3 Answers3

2

Try this code :

@Override
public void onBackPressed() {
        super.onBackPressed();
        post_content_web.loadUrl("about:blank");
}
Mohammad Nouri
  • 2,245
  • 4
  • 17
  • 34
2
 @Override
public void onBackPressed() {
    super.onBackPressed();
    webview.destroy();

}
Hardik
  • 21
  • 4
1

While Nouri's answer works exactly as the OP wants, for many you may want to stop video playback when a Fragment has been replaced or hidden--not just when the back button is pressed.

The solution is the same, but I recommend putting the code in onPause(). This will cover the above situations and the back button at the same time.

@Override
public void onPause() {
    super.onPause();
    post_content_web.loadUrl("about:blank");    // Stop video playback
}
SMBiggs
  • 11,034
  • 6
  • 68
  • 83