Android uses WebView display page and click Jump startactivity problem

Source: Internet
Author: User

The following is a personal humble opinion, the great God can directly ignore.

Directly into the theme, Android, a WebView control that everyone is particularly familiar with, can be used to load display Web pages, such as the mall's product details can be quickly realized with the Web page, but the recent project encountered a different problem, the product home page with WebView display, Click on a product or product category StartActivity jump to its own interface, homepage mixed part of the page loading will certainly encounter the interface display lag, show slow problem, but since the demand is so, can only do it.

Display a webpage does not say, first of all, first, optimize the display speed problem, that is, the display of text and pictures, in the OnCreate method plus the following code:

    if (Build.VERSION.SDK_INT >=) {            mwebview.getsettings (). setloadsimagesautomatically (true );         Else {            mwebview.getsettings (). setloadsimagesautomatically (false);        }

The Onpagefinished method in Webviewclient adds:

if (! mwebview.getsettings (). getloadsimagesautomatically ()) {                mwebview.getsettings (). setloadsimagesautomatically (true);            }

If you want to customize, you can load your own locally written HTML in the Onreceivederror method, because it may cause the page to display errors.

  

The second is to achieve startactivity click to jump to the Android interface, the following two words are extremely important:

Mwebview.getsettings (). setjavascriptenabled (true); Mwebview.addjavascriptinterface (New  webappinterface (mywebshopactivity. This), "startactivity");

Let your webview support page JS interaction, where "startactivity" can be changed, and the page JS method to keep consistent on the line, and then write you want to click the implementation of the effect, to display a word, or jump interface, etc...

 Public class webappinterface {        Context mcontext;         Webappinterface (Context c) {            = c;        }        @JavascriptInterface        publicvoid jump_product_detail (final  String ID) {           //todo processing code         }    }

I am here to jump click on the product, "Jump_product_detail" and the Above "startactivity" can be changed, but also to remain consistent.

Finally, in your custom processing code to say the possible problems, the front end in the Web page will add each click of the ID, that is, the identity, such as when you load the URL is "https://www.baidu.com/", the Click will change to "https:// Www.baidu.com/:01 "So much the URL of the ID, but then webview will load the URL once, but this URL is not there, so we have to reload the original URL, note that this can not be used Mwebview.loadurl (URL ) to load, see the log output below to know:

Java.lang.throwable:warning:a WebView method was called on thread ' Webviewcorethread '. All WebView methods must is called on the UI thread. The future versions of WebView could not be the support use on the other threads.

The treatment is replaced by:

Mwebview.post (new  Runnable () {    @Override    publicvoid  run () {        Mwebview.loadurl (URL); The URL is the initial value    }});

Instead of Mwebview.loadurl (URL) to load.

Again, the error uncaught Error:error calling method on Npobject when you start the interface with StartActivity, only needs to be modified to:

New Handler ();    Mhandler.post (new  Runnable () {    @Override    publicvoid  run () {       startactivity (New Intent (Action, URI));             });

will be able to better solve the above two easy-to-appear problems.

OK, first of all, what's the problem you can comment on below.

Android uses WebView display page and click Jump startactivity problem

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.