如何控制了Android的WebView历史/后退堆栈? [英] How to control the Android WebView history/back stack?

查看:1222
本文介绍了如何控制了Android的WebView历史/后退堆栈?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找出一种方法来处理的WebView回栈类似于如何在Android网页浏览器处理它时,后退按钮是从我自己的应用程序的的WebView pssed $ P $。

I am trying to figure out a way to handle the WebView back stack similar to how the Android web browser handles it when the back button is pressed from within my own app's WebView.

这个问题,是因为有的Javascript重定向。 web视图回堆栈似乎包含简单的URL重定向。这里是我的相关code:

The problem has to do with Javascript redirects. The WebView back stack appears to contain URLs that simply redirect. Here is my relevant code:

private class ArticleWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        view.loadUrl(url);
        return true;
    }
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (keyCode == KeyEvent.KEYCODE_BACK)
    {
        WebView wv = (WebView)findViewById(R.id.web);
        if (wv.canGoBack())
        {
            wv.goBack();

            return true;
        }
    }

    return super.onKeyDown(keyCode, event);
}

正如你所看到的,pressing后退按钮选择previous URL在堆栈中,shouldOverrideUrlLoading之后,在加载web视图本身()被调用。我其实在做内部shouldOverrideUrlLoading多很多(),它是不相关的这个特殊的问题,所以删除我的WebViewClient实施会不会为我工作。无论如何,当previous URL是一个Javascript重定向,该网址的负载,然后立即使用Javascript重定向到web视图只是在URL。我不能禁用Javascript的,因为网站依赖于它。我也不能更改网站(第三方)。

As you can see, pressing the back button selects the previous URL in the stack, which loads in the WebView itself after shouldOverrideUrlLoading() is called. I'm actually doing a lot more inside shouldOverrideUrlLoading() that isn't relevant for this particular question, so "remove my WebViewClient implementation" won't work for me. At any rate, when the previous URL is a Javascript redirect, that URL loads and then Javascript immediately redirects to the URL the WebView was just at. I can't disable Javascript because the website depends on it. I also can't change the website (third-party).

这个重定向问题,其结果是恶性循环的最终用户是谁疯狂pressing回只想比Android的网页处理引擎,速度稍快。

The result of this redirection issue is a vicious cycle for the end-user who is frantically pressing back just to be slightly faster than the Android webpage processing engine.

现在这里就是它变得有趣:Android的网页浏览器处理后退按钮就好了Javascript的重定向!它遵循重定向得很好,但后面的按钮的功能,用户希望它做的事。这意味着有可能正确地处理这种情况。如何在Android网页浏览器处理这个问题?

Now here is where it gets interesting: The Android web browser handles the back button just fine for Javascript redirects! It follows the redirects just fine but the back button does what a user expects it to do. That means it is possible to correctly handle this scenario. How does the Android web browser handle this problem?

推荐答案

我也有类似的问题,你发现如何弄明白。这里是我的回答

I had a similar problem with yours and found how to figure it out. Here is my answer.

当我点击一个链接(www.new.a),它会自动重定向其他链接(mobile.new.a)。通常情况下,链接重定向两个或三个,和我的解决方案已经制定上几乎每​​一个重定向的链接。我希望这个答案帮助您与annyoing重定向连接。

When I click the first link(www.new.a) it automatically redirects other link(mobile.new.a). Usually the links redirect two or three, and my solution have been worked on almost every redirect links. I hope this answer help you out with annyoing redirecting links.

我终于想通了这一点。你需要有四个API的WebViewClient。有shouldOverrideUrlLoading(),onPageStarted(),onPageFinished(),和doUpdateVisitedHistory()中的WebViewClient。您需要的所有的API是API 1,因此不用担心。

I finally figured out that. You need a WebViewClient with four APIs. There are shouldOverrideUrlLoading(), onPageStarted(), onPageFinished(), and doUpdateVisitedHistory() in the WebViewClient. All the APIs you need is API 1 so don't worry about.

下面是我的回答。检查出的! :)

Here is my answer. Check out that! :)

这篇关于如何控制了Android的WebView历史/后退堆栈?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆