2011-09-13 74 views
3

我正在使用以下代码在我的Android应用中显示webview。在webview中启用后退按钮

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

有人能帮助我的解决方案?

+0

取代它虽然你提到你试图在链接中提到的例子,你也覆盖onBackPressed方法? –

+0

感谢您的评论Rahul。我编辑了我的问题,并添加了我正在使用的示例中的代码。实际的onKeyDown方法可以工作,因为我可以在按下后退按钮时显示吐司,但是当我按下后退按钮时使用应用程序崩溃之上的代码时。我尝试了onBackPressed,但同样的事情发生。 – Brigante

+0

你说应用程序崩溃,你也可以发布你的崩溃转储,日志猫.. –

回答

7

Got it!你在这行

WebView webview = new WebView(this); 

而不是使用你的成员变量要创建内部函数变量的问题,因此你的成员变量的onkeydown函数内部空。

只是

webview = new WebView(this); 
+0

这样做了!非常感谢你的帮助!! – Brigante