Android WebView cannot work normally due to redirection () solution, webviewgoback

Source: Internet
Author: User

Android WebView cannot work normally due to redirection () solution, webviewgoback


Android WebView cannot work properly due to redirection () Solution


First let's talk about the problem, the initial Page is A, click A link to jump to B (http://xxx.com.cn/), B page redirects to C page (http://xxx.com.cn/website/index.html)

When webview. goBack () is called, the page is rolled back to B and then redirected back to page C.

This will cause two problems:


1. The initial page A of webview cannot be rolled back.

2. Unable to exit Activity or Fragment normally (the page can be exited only when C is not loaded yet)


I have summarized the following three methods for solving this problem, which can be used as needed:

I. first, you need to communicate with the front-end developers to see if redirection is necessary. If the redirection link is only a domain name and then redirected to the domain name/index.html by default, this redirection is meaningless.

You just need to connect the web page, such

<a href="http://xxx.com.cn"/>

Replace directly

<a href="http:///xxx.com.cn/index.html"/>

To solve the problem.


2. redirection is required on the page. Therefore, we need to maintain a history stack of webview by ourselves, and filter and redirect or reload the page as needed:

If the current link is redirected, You need to ignore the link at the upper level when performing the rollback. You do not need to use webview. goback () to remove the redirected and redirected URLs,

Obtain the initial Page Link and perform the loadUrl () operation on your own.


3. There is another method. Similar to method 2, you need to maintain the history stack of webview by yourself. However, you need to cooperate with the front-end to provide js functions to obtain whether the webpage is redirected.

When filtering URLs in the webviewClient callback shouldoverloading (), if the url belongs to the redirection address, it is not added to the stack. Load the url based on the historical stack during rollback.


Method 2:

First, define a history stack:

private ArrayList<String> loadHistoryUrls = new ArrayList<String>();

Add the initial page Url

loadHistoryUrls.add(INITAL_WEB_URL);

Then add the loaded url:

Public boolean shouldOverrideUrlLoading (WebView view, String url) {// add the filtered url to the historical stack loadHistoryUrls. add (url); return true ;}

Finally, process in webview. goback:

@ Override public boolean onKeyDown (int keyCode, KeyEvent event) {// determines whether the operation if (webView. canGoBack () & event. getKeyCode () = KeyEvent. KEYCODE_BACK) {// filter if (loadHistoryUrls. size ()> 0 & loadUrls. get (loadHistoryUrls. size ()-1 ). contains ("index.html") // remove the last two links in the loading stack loadHistoryUrls. remove (loadHistoryUrls. get (loadHistoryUrls. size ()-1); loadHistoryUrls. remove (loadHistoryUrls. get (loadHistoryUrls. size ()-1); // load the webview page before redirection. load (loadUrls. get (loadHistoryUrls. size ()-1); return true ;}}}


After loading the stack, we found that webview also has the corresponding API:

// Obtain the history list WebBackForwardList mWebBackForwardList = webView. copyBackForwardList ();

However, this api may be affected by the system version or modified by different mobile phone systems.

To solve this problem, you can maintain the loaded stack or directly call the system api as needed.


Here, we will summarize that if redirection is not necessary, solution 1 is adopted, which is the simplest, with a small amount of modification. If redirection is necessary, solution 2 or solution 3 will be used.

Because of the need to interact with front-end personnel, the communication, development, and maintenance costs required by solution 3 are much higher than that required by solution 2, but the judgment on whether to redirect is very accurate, if multiple redirects exist, you do not need to change the code after one development. solution 2: Write the url to be filtered out. If multiple redirection occurs, the code will appear bloated and the code needs to be added again each time. the specific usage depends on the development of the project.


If there is reprint, please declare the Source: when the sand: http://blog.csdn.net/t12x3456



Question returned on the android webview page? The following describes in detail how to do this.

No, my webview won't have your problem. If there is such a problem, isn't the back function useless?
 
Android development, learning, using webview to access Youku, cannot play videos, error messages, and other answers

Add in the code
Webview. getSettings (). setJavaScriptEnabled (true); // supports js
Webview. getSettings (). setPluginsEnabled (true); // sets the webview support plug-in.

In addition, android: hardwareAccelerated = "true" Hardware acceleration must be added to the application of the Manifest configuration file to enable video playback.

Related Article

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.