How to solve the memory leak when using webview in Android
Most people do not understand the knowledge points of this article "how to solve memory leaks when using webview in Android", so the editor summarizes the following contents, detailed contents, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this article "how to solve memory leaks when using webview in Android".
1. Avoid writing webview controls directly in xml, which references activity, so write a LinearLayout in xml, and then linearLayout.addView (new MyWebview (getApplicationContext ()
In this way, dynamic generation of webview can avoid memory leaks, but this will cause some models of webview to click on the hyperlink and cause the program to crash. The temporary solution is to prohibit clicks and need to rewrite webview.
Public class MyWebview extends WebView {public MyWebview (Context context) {super (context);} public MyWebview (Context context, AttributeSet attrs) {super (context, attrs);} public MyWebview (Context context, AttributeSet attrs, int defStyleAttr) {super (context, attrs, defStyleAttr);} @ Override public boolean onTouchEvent (MotionEvent event) {return false;}}
This avoids program crashes.
Manual release of webview memory is required when 2.activity is closed
@ Override protected void onDestroy () {super.onDestroy (); if (webview_projectinfo! = null) {webview_projectinfo.removeAllViews (); webview_projectinfo.destroy (); webview_projectinfo = null; ll_webview.removeAllViews (); ll_webview = null }} the above is about the article on "how to solve memory leaks when using webview in Android". I believe everyone has a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.