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
According to data from the World Health Organizat...
For automobile manufacturers, the vast China is n...
On January 23, the Central Meteorological Observa...
It is the peak season for pollen allergies again....
In the vast world of food, there is a magical phe...
Remington files for bankruptcy Remington, an Amer...
Cargo spacecraft are the backbone of manned space...
How can ordinary people create a community that c...
Source: Dr. Curious The cover image of this artic...
Nonsense: Time flies. I have been working in SEO ...
The concept of growth hacking is very popular now...
I believe many friends still remember "PP As...
For more than a century, scientists have found th...
In recent years, as the cost of drone development...
What is New Marketing? There are four key words: ...