的WebView返回,刷新,前进?根本不起作用! [英] WebView Back, Refresh, Forward? Simply doesn't work!

查看:629
本文介绍了的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的'。有没有错误codeS但是,没有这些方法其实做任何事情。

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);
}

}

推荐答案

在code您共享不包括在其中创建的WebView和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 是它的一部分,你已经证明了你的问题类的实例字段。难道说 web视图正在每次你导航到一个新的页面时重现?也就是说,code是这样的:

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/");

如果这是正在做什么,你需要的是创建'web视图一次,只需要调用使用loadURL 每次你需要导航到新的URL。通过这种方式,web视图实例将能保留的历史。

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天全站免登陆