Some pits used by WebView in Android

preface

In Android development, WebView is often used to display web pages, start your own browser in activiry, or simply display some online content.

WebView can help us display HTML pages well, but some problems may arise if WebView is not used properly. Here are my optimization skills in the following aspects

1. The activity of WebView can be displayed in another process, which can be separated from the main process of our app. Even if the WebView has oom crash and other problems, it will not affect the main program. How to implement it is actually very simple. Just add Android: process = "packagename. Web" to the activity tag of Android manifest.xml. When running, you will find an additional process, ha ha.

2. The creation of WebView is also tricky. It's best not to use WebView in layout.xml. You can dynamically add view (WebView) to the container through a ViewGroup container with code, so that you can clean up the memory of WebView in ondestrory(). In addition, you need to note that the creation of WebView requires the use of ApplicationContext instead of the context of activity, We should all know that the activity object is no longer occupied during destruction. When you finally leave, you need to destroy the WebView in time. In ondestory(), you should remove the WebView from the ViewGroup first, and then call WebView. Removeallviews(); webview.destory();

establish

Destroy

3. Further optimization, after the activity is killed passively, it's better to save the WebView state, so that the user can see the previous state when opening it next time. Well, that's it. WebView supports savestate (bundle) and restorestate (bundle) methods, so it's simple. Ha ha, look at the Code:

Save status:

Recovery status:

In oncreate (bundle savedinstancestate) of activity, the following is used:

Sum up a few more pits

WebViewClient.onPageFinished() 。 You can never be sure whether the web content is really loaded when WebView calls this method. This method may be called many times when the currently loaded web page jumps. There is a specific explanation on stackoverflow (how to listen for a WebView finishing loading a URL in Android?), but the solutions listed therein are not perfect. Therefore, when your WebView needs to load various web pages and take some actions when the page is loaded, webchromeclient. Onprogresschanged() may be more reliable than webviewclient. Onpagefinished().

WebView background power consumption problem. When your program calls WebView to load web pages, WebView will start some threads (?) by itself. If you do not destroy WebView correctly, these residual threads (?) will always run in the background, resulting in high power consumption of your application. The processing method I adopt is lazy, simple and rough (not recommended), that is, directly call system. Exit (0) in activity. Ondestroy() to completely remove the application from the virtual machine, so that there will be no problems.

Switch WebView flash screen problem. If you need to switch back and forth between different webviews (including different web content) in the same ViewGroup, you will find that flashing is inevitable. This should be the bug of Android hardware acceleration. If you turn off hardware acceleration, the situation will be much better, but you can't get a good browsing experience. You will feel that the web page slides one card at a time.

Data accumulation. Turning on caching is conducive to the web browsing experience, but you will find that even if you clear the necessary contents, such as cache, cookie, form data, history, password and so on, your application will occupy more and more storage space. Finally, you have to manually clear the data in the application information interface set by the system:(

Scroll bar problem. The horizontal scroll bar of Android system WebView is really thick, wooden and

On some mobile phones, when WebView has videos, after the activity is destroyed, the video resources are not destroyed, and even can be heard playing in the background. Even destroying webviews like just now won't help. The solution: change the URL to an empty address before ondestory. Isn't it a pit?

summary

The above is the whole content of this article. I hope the content of this article has a certain reference value for your study or work. If you have any questions, you can leave a message. Thank you for your support for programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>