Android WebView (iii) webviewclient

Source: Internet
Author: User


WebView Self-loading sub-pagesBy default, when you click the Hyperlink button on a page loaded by WebView, you start the system's own browser to load the new page. If you want to get webview to load itself,
You need to add the following code:
Webview.setwebviewclient (New Selfwebviewclient ());

Selfwebviewclient inherits from Webviewclient, as follows:
public class Selfwebviewclient extends Webviewclient {@Overridepublic void onpagefinished (WebView view, String URL) {Supe r.onpagefinished (view, URL);//Load End} @Overridepublic void Onreceivederror (WebView view, int errorcode,string Description, String failingurl) {super.onreceivederror (view, ErrorCode, description, Failingurl);// The Load Failure page loadfailure.setvisibility (view.visible) is displayed here:} @Overridepublic void Onloadresource (WebView view, String URL) {super.onloadresource (view, URL);//Load resource} @Overridepublic void Onreceivedhttpauthrequest (WebView view,httpauthhandler handler, string host, String realm) { Super.onreceivedhttpauthrequest (view, Handler, host, realm)//webview save password} @Overridepublic void onpagestarted ( WebView view, String URL, Bitmap favicon) {super.onpagestarted (view, URL, favicon);//Start Loading page} @Overridepublic Boolean Shouldoverrideurlloading (WebView view, String URL) {//Click the link jump to respond to the hyperlink button in the Web page. View.loadurl (URL); return true}}

The general life cycle of the webviewclient is as follows:
If the load succeeds
Onpagestarted-->> onloadresource-->>onpagefinished-->> Onloadresource
If the load fails
Onpagestarted-->>
Onloadresource-->>onreceivederror-->>onpagefinished-->>onpagefinished

The common use of webviewclient has three: 1, let WebView self-loading sub-page, 2, if the WebView load failure display failure page; 3. Remember page
Login password. The above three features are briefly described in the above code.
WebView returning from a sub-pageThe above describes how to load a sub-page directly with WebView, but how to return to the previous page from a child page?
This needs to have a departure time, I will use the return key to trigger, the code is as follows:
  @Override Public  boolean onKeyDown (int keycode, keyevent event) {       if (keycode = = Keyevent.keycode_back) && Amp Webview.cangoback ()) {           webview.goback ();//goback () indicates return of the previous page of WebView return           true;       } else{      Finish ();      }      return false;   



Android WebView (iii) webviewclient

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.