4

I detected the next issue and maybe one of you can help me.

I created a WebView and loaded a page that use WebRTC technology in order to establish video-conference between two devices.

Everything was OK up this point. Now I want to change my network connection (for instance from 3G to Wi-Fi), and the problems appears. The media streaming is not working. This streaming is over a P2P connection between two devices.

To avoid it, my first approach was destroying the WebView and regenerating it again, but didn't work.

I believe that some process that WebView starts keeps in memory, because if I delete in a hard mode the process depending on the activity, it works, but it is not elegant in a release version.

How will be the best solution to create again the WebView, or at least to destroy it?

Here it is my poor solution. I created the WebView programmatically, without included in activity XML view. I read about memory leaks and timers on WebView, but it didn't work. I also used the ApplicationContext to create the WebView.

public void destroyWebView() {

    Log.d(TAG, "Destroy webView");
    if(webView != null) {
        webView.removeAllViews();
        webView.clearHistory();
        webView.clearCache(true);
        webView.loadUrl("about:blank");
        webView = null;
        parentWebView.removeAllViews();
    }

}

@Override
protected void onDestroy() {
    Log.d(TAG, "*** ON DESTROY");
        if (webView != null) {
            destroyWebView();
        }
        android.os.Process.killProcess(android.os.Process.myPid());
        super.onDestroy();
}
NaijaProgrammer
  • 2,892
  • 2
  • 24
  • 33
  • Have you seen http://stackoverflow.com/questions/17418503/destroy-webview-in-android ? – Mikhail Naganov Feb 03 '16 at 20:26
  • @MikhailNaganov yes, previous to write this question I have seen it. But not works for me. Something strange happens with webview process. – Carlos Morell Feb 04 '16 at 08:44
  • OK, just note that the statement from that answer about "`webView = null` is equivalent to `webView.destroy()`" is not correct. You should call `webView.destroy()` after you have removed it from the views hierarchy (I assume, this is what `parentWebView.removeAllViews()` does). Thus the right sequence of steps is: 1) `removeAllViews()`; 2) call `webView.destroy()`; 3) `webView = null`. No need to call `webView.loadUrl("about:blank")`. – Mikhail Naganov Feb 04 '16 at 16:16

0 Answers0