WebView's onPause() not Working

Viewed 719

I have WebView inside an activity that extends AppCompatActivity, I'm trying to stop Audio playback inside webview when the Activity is closed/swapped but it's not working, the sound keeps playing all the time, below in my code

public class PostDetailsActivity extends AppCompatActivity{
    WebView mArticleText;
    ...
    void loadDatatoWebView(String html){
        String base64 = Base64.encodeToString(html.getBytes(), Base64.DEFAULT);
        mArticleText.loadData(base64, "text/html; charset=utf-8", "base64");
        mArticleText.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if (url != null) {
                    // open links in external browser
                    view.getContext().startActivity(
                            new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
                    return true;
                } else {
                    return false;
                }
            }


        });
    }

    @Override
    protected void onPause() {
        super.onPause();
        mArticleText.pauseTimers();
        mArticleText.onPause();
    }

    @Override
    protected void onResume() {
        super.onResume();
        mArticleText.onResume();
    }
} 

I tried adding WebView.loadUrl("about:blank")and it solved the playback problem but introduced some UX problems, the webview had to load again when the activity is resumed ant causes an annoying delay as rendering could take some time.

0 Answers
Related