WebView后退,刷新,前进?根本行不通! [英] WebView Back, Refresh, Forward? Simply doesn't work!

查看:251
本文介绍了WebView后退,刷新,前进?根本行不通!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尽一切努力使我的前进"和后退"开始工作.

I have tried everything to get my 'forward' and 'back' to work.

刷新工作正常[我通过将方法更改为读取'webView.reload();来解决它.而不是"webView.refresh();"

Refresh is working [I figured it out by changing the method to read 'webView.reload();' instead of 'webView.refresh();'

有人可以协助前进和后退吗?我已经尝试过前进","canGoForward"和"goForward"以及后退","canGoBack"和"goBack".没有错误代码,但是这些方法实际上都不做任何事情.

Can anyone assist with the forward and back? I have tried 'forward', 'canGoForward' and 'goForward' as well as 'back', 'canGoBack' and 'goBack'. There are no error codes however none of those methods actually do anything.

public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu); // Add menu items, second value is the id, use this in the onCreateOptionsMenu
    menu.add(0, 1, 0, "Back");
    menu.add(0, 2, 0, "Refresh");
    menu.add(0, 3, 0, "Forward");
    return true; // End of menu configuration
}
public boolean onOptionsItemSelected(MenuItem item){ // Called when you tap a menu item
    switch (item.getItemId()){
        case 1: //If the ID equals 1, go back
            webView.goBack();
        return true;
        case 2 : //If the ID equals 2, refresh
            webView.reload();
        return true;
        case 3: //If the ID equals 3, go forward
            webView.goForward();
        return true;
        }
    return false;
    }
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) { // Enables browsing to previous pages with the hardware back button
    if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) { // Check if the key event was the BACK key and if there's history
        webView.goBack();
        return true;
    }   // If it wasn't the BACK key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}

}

推荐答案

您共享的代码不包括创建Web视图和在URL之间导航的部分.所以我只是猜测可能发生的事情.

The code you shared doesn't include the portion where you are creating webview and navigating between urls. So I am just taking a guess at what might be happening.

似乎 webview 是您的类的实例字段,您在问题中已经显示了该部分.可能是每次您导航到新页面时都会重新创建 webView 吗?也就是说,代码类似于:

It seems that webview is an instance field of your class whose portion you have shown in the question. Could it be that webView is being recreated every time you navigate to a new page? That is, the code is something like:

webview = new WebView(this);
webview.loadUrl("http://slashdot.org/");

如果正在执行此操作,则只需创建一次"webView",然后每次需要导航至新的url时都调用 loadUrl .这样,webview实例将能够保留历史记录.

If this is what is being done, all you need is to create the 'webView' once and just call loadUrl everytime you need to navigate to new url. This way the webview instance will be able to retain the history.

这篇关于WebView后退,刷新,前进?根本行不通!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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