Why use WebView? With the continuous development of app business, it is unrealistic to rely solely on native code to pile up functions. After all, the development time will increase, and both iOS and Android need to be developed at the same time. Moreover, if the UI is changed a little bit, a package needs to be moved (although Android can now perform hot updates, hot updates are not 100% effective. Anyone who has understood the principle will know it). In the end, we will choose to use native nested H5 for development. In this way, we can change the UI at any time and expand functions arbitrarily. Then, we will use Android's WebView, the control that makes us both sad and happy. Nowadays, mobile phones are highly customized, and the native code of the system will be modified and added to a certain extent. The effects presented by WebView of different mobile phones are also different, which can be said to be colorful, so I understand the pain of Android developers. Next, I will explain my experience of using WebView in the project in detail. If you don't like it, please don't criticize me. use At the beginning, I believe everyone added the WebView control directly to the layout file. Of course, I did the same at the beginning, just for simplicity, and I didn’t know what problems would arise.
Just add a WebView like this, and you will find that there is no problem, it can be displayed as usual, and everything is normal. When you repeatedly open the page with WebView, you will find that the memory of the application will continue to increase, and it will not drop after being destroyed, and it will not drop even if you click GC. This is a memory leak. At this time, you will find that it is incorrect to use WebView in this way, so what is the best way to use it? That is to add it dynamically in the code. First, declare a parent layout in the layout file
Then in the code, add WebView as its child View
Many people on the Internet say that this context should use the application. I think it is wrong. What if your WebView needs to pop up a dialog? There are other unpredictable problems. It is best to use the Context of the current activity. The above is about how to add WebView for use, and then what properties of it do we need to use in development?
The above are the most basic settings for using WebView. I believe that the above settings will be performed during the development process.
Then there is the interaction between WebView and JS
When the activity executes the life cycle, it is important to note that WebView needs to be destroyed during onDestroy, otherwise memory leaks will occur.
You can see that the system version is judged in the onDestroy method above. That's because I tested it in different versions. If the WebView version is lower than 5.0, if the WebView is removed from the parent first, the WebView will not be destroyed, which will cause memory leaks. You can try it yourself to see if this statement is correct. Another problem we are facing now is that when WebView is nested in ScrollView, some models will have screen flashing problems, which will not happen when using WebView alone. After turning off hardware acceleration, the user experience is not good. Therefore, we have not thought of a better solution yet, so it is recommended not to nest controls such as WebView in ScrollView. |
<<: Things about memory optimization in Android - a record of image optimization
>>: Android full set of animation usage tips
I have been searching for spring for half my life...
Course Catalog: ├──Section 01 What are the common...
In the blink of an eye, it is the end of March. T...
There are a wide variety of Baidu backend optimiz...
I've been receiving a lot of inquiries about ...
September 30 News Messages is a feature update la...
Dahe.com (Reporter Mo Shaohua) "Reporter, ca...
Recently, with the release of China's smartph...
For e-commerce platforms, accurately identifying ...
01 Brands are losing marketing initiative In the ...
Written by: Wu Tingting Editor: Kou Jianchao Layo...
1.How many ads can one account create? In one acc...
The picture is produced by "Science Refutes ...
We often hear some design experts share with us t...
Application Management FAQ Q1. What is the regist...