Android启用webview中的后退按钮

时间:2011-09-13 10:33:57

标签: java android webview

我正在使用以下代码在我的Android应用中显示网页视图。

package com.company.myapp;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class ArticlesActivity extends Activity {

    /** Initialize the Google Analytics Tracker */
    GoogleAnalyticsTracker tracker;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        WebView webview = new WebView(this);
        setContentView(webview); 
        setProgressBarVisibility(true); 
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        final Activity activity = this;
        tracker = GoogleAnalyticsTracker.getInstance();
        // Start the tracker, updating google every 20 seconds
        tracker.start((String) getText(R.string.analyticsID), 20, this);

        webview.setWebChromeClient(new WebChromeClient() { 
          public void onProgressChanged(WebView view, int progress) { 
            activity.setProgress(progress * 100 ); 
          } 
        }); 

        webview.setWebViewClient(new WebViewClient() { 
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }
        });
        webview.loadUrl("http://www.google.com");
    }
    @Override
    public void onResume() {
        tracker.trackPageView("ArticlesActivity");
        super.onResume();
    }
    @Override
    protected void onDestroy() {
      super.onDestroy();
      // Stop the tracker when it is no longer needed.
      tracker.stop();
    }
}

如果历史记录存在,我需要启用后退按钮,而不是退出webview。

I've tried many different code examples such as this但无法正常工作。当按下后退按钮时,应用程序才会关闭。

这是我的代码与后退按钮代码,但它只是在按下后退按钮时崩溃应用程序:

package com.company.myapp;

import com.google.android.apps.analytics.GoogleAnalyticsTracker;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class ArticlesActivity extends Activity {

    WebView webview;

    /** Initialize the Google Analytics Tracker */
    GoogleAnalyticsTracker tracker;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_PROGRESS);
        getWindow().setFeatureInt(Window.FEATURE_PROGRESS, Window.PROGRESS_VISIBILITY_ON);
        WebView webview = new WebView(this);
        setContentView(webview); 
        setProgressBarVisibility(true); 
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
        final Activity activity = this;
        tracker = GoogleAnalyticsTracker.getInstance();
        // Start the tracker, updating google every 20 seconds
        tracker.start((String) getText(R.string.analyticsID), 20, this);

        webview.setWebChromeClient(new WebChromeClient() { 
          public void onProgressChanged(WebView view, int progress) { 
            activity.setProgress(progress * 100 ); 
          } 
        }); 

        webview.setWebViewClient(new WebViewClient() { 
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                Toast.makeText(activity, "Oh no! " + description, Toast.LENGTH_SHORT).show();
            }
        });
        webview.loadUrl("http://www.google.com");
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {

            webview.goBack();

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

    @Override
    public void onResume() {
        tracker.trackPageView("ArticlesActivity");
        super.onResume();
    }
    @Override
    protected void onDestroy() {
      super.onDestroy();
      // Stop the tracker when it is no longer needed.
      tracker.stop();
    }
}

有人可以帮我解决问题吗?

1 个答案:

答案 0 :(得分:7)

知道了!你在这一行的问题

WebView webview = new WebView(this);

您没有使用您的成员变量,而是在函数内部创建变量,因此您的成员变量在onKeyDown函数内为null。

只需用

替换它
webview = new WebView(this);