A summary of some of WebView's experiences

Source: Internet
Author: User

Part of the content re-recorded in: http://www.360doc.com/content/14/0611/13/15210553_385676271.shtml

A Loading resources is not slow, but the resources are much slower. Pictures, CSS, JS, html each of these resources probably need to 10-200ms, generally 30ms is OK. If you have a lot of resources on a page, it's a waste of time.

Two The execution speed of Js and CSS.

Webview loading page order is like this: first load HTML, and then parse from the inside CSS, JS files and pages on the page to write dead image resources to load, if the WebKit cache inside, do not load. After these resources are loaded, CSS rendering and JS execution are performed. CSS rendering generally does not take a long time, dozens of milliseconds is OK. The key is the execution of JS, if used jQuery, it takes 5-6 seconds to execute. And during this time, if not set the background in the WebView, the webpage part is white, it is very difficult to see. This is a very bad user experience. So if you use the Web layout program, it is best not to use a large JS frame.

Three Cross-tuning between Web pages and Java

. Java calls JS inside the function, the speed is not satisfactory, about one hundred or two hundred milliseconds at a time, if you want to do a very interactive thing, this speed can be crazy. And the reverse is not the same, JS to tune the Java method, fast, basically 40-50 milliseconds at a time. So try to use JS to call the Java method, rather than Java to invoke the JS function.

. Java calls the JS function, there is no return value, and JS calls the Java method, you can have a return value. The return value can be a string, or it can be an object. If it's a string, there's a nasty question, and 3rd I'll talk about. If it is an object, the object will be converted to a JS object, which can be accessed directly inside the method. But I do not recommend that Java be returned to JS is the object, unless it is necessary. Because JS receives objects returned by Java, there are some swap objects, and if the number of these objects increases to 500 or more than 600, the program will have problems. So try to return the base data type or string.

. Js call Java method, the return value if it is a string, you will find that the string is native, you can not make some modifications to it, such as want to substr it, not access. How to solve it? Turn into the locale. You can use the toLocaleString () function. However, this function is not fast enough, and it can be time-consuming to convert many strings.

Four Drag the element on the page.

If you use the Web practices on your PC, listen to OnMouseDown, onmousemove, and onmouseup. On the phone, the event model is different. Click on the page, drag, and then release, hand off the screen, WebView will trigger OnMouseDown, onmousemove, onmouseup events. So, to drag, you can't do that. The problem bothered me for a long time, and then I found out what I did on my iphone. The WebView on the Iphone has events Ontouchstart, Ontouchmove, and ontouchend designed for touch screens that respond in real time to solve the drag problem.

Five. Webview inside the page, if there is input, need to enter, but point up but did not respond, input method does not come out. This is because WebView does not get the focus. You need to set Requestfocus () in Java for WebView.

Some methods that can be overridden

1, an event that receives an HTTP request
Onreceivedhttpauthrequest (WebView view, Httpauthhandler handler, string host, String realm)

2, open the event before the link
public boolean shouldoverrideurlloading (WebView view, String URL) {view.loadurl (URL); return true;}

This function we can do a lot of things, such as we read to some special URL, so we can not open the address, cancel the operation, for the pre-defined other operations, which is very necessary for a program.

3. Events loaded on page completion
public void onpagefinished (WebView view, String URL) {}

Similarly, we know that a page loading is complete, so we can close the loading bar, switch the program action.

4, loading the start of the page event
public void onpagestarted (WebView view, String URL, Bitmap favicon) {}

This event is the start of loading the page call, usually we can set a loading in this page, tell the user program is waiting for the network response.

Through these events, we can easily control the operation of the program, while using the browser to display content, while monitoring the user operation to achieve the various display methods we need, but also to prevent the user to create the wrong operation.

A summary of some of WebView's experiences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.