Android WebView memory leak pit

Symptom

After an autoplay sound page H5 access, Activity carrying H5 page is still playing in the background after the finish sound, can not be stopped, only the end of the process or the App acquisition system audio focus in another place so that the sound webview audio player loses focus sound stopped playing.

Troubleshooting steps

  1. Chrome, use chrome://inspectthe link ADB commissioning
  2. Activity H5 finish bearer page
  3. Chrome observed H5 page is still retained, and no garbage collection

solution

GC corresponds webview not recovered after this issue is very clear where the Activity is --WebView finish off.
So it should be active during the recovery WebView finish the corresponding Activity.
But not in a simple and crude method onDestory Activity recovered in the webview, as follows:

1
2
3
4
5
6
7
8
 
protected void ()
{
if (webView!= null)
{
webView.destroy();
}
}

This issue will lead to that, when executed Activity onDestory, possible internal webview also attached to the parent control, then when the question of the establishment, the following exception is thrown, causing the program to stop running:
Error: WebView.destroy() called while still attached

Therefore, the correct solution to this problem should be in the Activity of onDetachedFromWindowthe treatment method, a treatment protocol as follows:

1
2
3
4
5

public void onDetachedFromWindow() {
super.onDetachedFromWindow();
webView.destroy();
}

Original: Big Box  Android WebView memory leak pit


Guess you like

Origin www.cnblogs.com/chinatrump/p/11606400.html