Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/222.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java WebView canGoback()功能上的WebView clearHistory()?_Java_Android_Webview - Fatal编程技术网

Java WebView canGoback()功能上的WebView clearHistory()?

Java WebView canGoback()功能上的WebView clearHistory()?,java,android,webview,Java,Android,Webview,我试图使用历史记录作为退出应用程序的检查。“主页”按钮将页面重新加载到google.com,但即使在我调用.clearHistory()之后,仍然保留页面历史记录 以下是菜单处理/选择的代码: @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle item selection switch (item.getItemId()) { case R.id.Home: mWebvi

我试图使用历史记录作为退出应用程序的检查。“主页”按钮将页面重新加载到google.com,但即使在我调用.clearHistory()之后,仍然保留页面历史记录

  • 以下是菜单处理/选择的代码:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      // Handle item selection
      switch (item.getItemId()) {
        case R.id.Home:
          mWebview.loadUrl("http://google.com");
          mWebview.clearHistory();
          return true;
        case R.id.About:
          Toast.makeText(this, "TEXT", Toast.LENGTH_SHORT).show();
        return true;
        default:
          return super.onOptionsItemSelected(item);
      }
    }
    
  • 以下是退出功能的代码:

    @Override
    public void onBackPressed() {
      if (mWebview.canGoBack()) {
        mWebview.goBack();
      }
      else {
        super.onBackPressed();
      }
    }
    
  • 根据您需要在页面完成加载时调用clearHistory:

    历史记录似乎会清除当前页面之前的所有内容 因此,如果您的浏览器位于“A”页,您可以清除历史记录并导航到 “B”页您的历史记录将是“A”“B”,而不仅仅是“B”,如果您清除了 历史记录当“B”完成加载时,您将只有“B”

    因此,您不应该在
    loadUrl
    之后立即调用
    clearHistory
    ,而应该从
    onPageFinished
    onProcessChanged
    中调用它