的WebView回到历史不重定向 [英] WebView back history without redirects

查看:1789
本文介绍了的WebView回到历史不重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现机器人的WebView和的onkeydown方法返回键。 (它实现了 webview.goBack();

I implemented android webview and onKeyDown method for back key. (It implements webview.goBack();)

我的问题是完全类似于下面这个职位的问题(没有答案的)

My problem is exactly similar to the question in this post below (no answers there)

<一个href="http://stackoverflow.com/questions/14390908/how-to-control-the-android-webview-history-back-stack">How控制了Android的WebView历史/后退堆栈?

问题 - 当我preSS后退按钮,的WebView 选择previous网址,但如果该URL实际上是一个重定向,它进入这种恶性周期/循环。如果你看一下Chrome或股票的浏览器,它可以正确处理回不回去的重定向。

PROBLEM - When I press back button, webview selects the previous URL, but if that URL was actually a redirect, it goes into this vicious cycle/loop. If you look at chrome or stock browser it correctly handles the back without going back to the redirects.

这又如何解决呢?

例:去gap.com。然后选择我的差距信用卡。这将打开一个重定向链接,然后在最后一页。现在,当我点击回来,它永远不会去Gap.com主页。

Example: go to gap.com. Then select "My Gap Credit Card". This opens a redirect link and then the final page. Now when I click back, it never goes to Gap.com home page.

任何建议...

补充说明:我做贯彻落实 shouldOverrideUrlLoading 。如果我删除的方法,它似乎工作正常,但用这种方法,不...

Additional Information: I did implement the shouldOverrideUrlLoading. If I remove that method, it seems to work fine but with this method it does not...

推荐答案

我刚刚测试过这对豆形软糖,它似乎工作。

I've just tested this on jellybean and it seems to work.

从本质上讲,每当一个新的URL在web视图加载保持URL的副本。

Essentially, whenever a new URL is loaded in the WebView keep a copy of the url.

在接下来的URL请求,仔细检查他们,我们是不是已经在此页上,如果是这样,那么在web视图历史回去又迈进了一步。

On the next URL request, double check they we aren't already on this page, if they are, then go back in the webview history another step.

从本质上讲,这是依靠传递到覆盖一步的网址是重定向的URL,而不是最终的重定向的URL。

Essentially this is relying on the url passed into the override step being the redirected url, rather than the final redirected url.

public class MainActivity extends Activity {

    private Button mRefreshButton;
    private WebView mWebView;
    private String mCurrentUrl;

    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        setContentView(R.layout.main);

        mWebView = (WebView) findViewById(R.id.webview);
        mRefreshButton = (Button) findViewById(R.id.refresh);

        mRefreshButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                 mWebView.reload();
            }
        });

        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mWebView.setWebViewClient(new WebViewClient() {

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                if(mCurrentUrl != null && url != null && url.equals(mCurrentUrl)) {
                    mWebView.goBack();
                    return true;
                }

                view.loadUrl(url);
                mCurrentUrl = url;
                return true;
            }
        });

        mWebView.loadUrl("http://www.gap.com/");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN) {
            switch(keyCode) {
                case KeyEvent.KEYCODE_BACK:
                    if(mWebView.canGoBack()){
                        mWebView.goBack();
                        return true;
                    }
                    break;
            }

        }
        return super.onKeyDown(keyCode, event);
    }
}

这篇关于的WebView回到历史不重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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