Webview Video doesn't stop on Backpress - Android

2k views Asked by At

In my android application I want to open a webpage in a Webview. A webpage which i want to open also contains video. To achieve this functionality, I used WebChromeClient in my application. The problem is that, OnBackPressed() of this activity, some videos don't get stopped and they keep playing in background, though my Webview activity is finished. I don't know why this happens. I tried various codes, but couldn't solve this problem. Some videos stop automatically, but some keeps on playing in background. How to solve this issue ?

Please help me to solve this problem.

Here is my code :

@Override
    protected void onDestroy() {
        super.onDestroy();
        Log.e("Test HTML5:-", "onDestroy Called...");
        mWebView.stopLoading();
        // mWebView.clearCache(true);
        // mWebView.clearHistory();
        // mWebView.pauseTimers();
        abc.removeView(mWebView);
        mWebView.removeAllViews();
        mWebView.destroy();
        mWebView = null;
        finish();
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }
2

There are 2 answers

3
Biraj Zalavadia On

write this code on onPause()

@Override
    protected void onPause() {
        super.onPause();
        Log.e("Test HTML5:-", "onDestroy Called...");
        mWebView.reload();
        Intent i = new Intent(getApplicationContext(), MainActivity.class);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
        i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(i);
    }
0
mesutpiskin On

Use to onBackPressed

 @Override
    public void onBackPressed() {
        mWebView.stopLoading();
        mWebView.removeAllViews();
        mWebView.destroy();
        mWebView = null;
        finish();
        super.onBackPressed();
    }