Java – Android enables the back button in WebView

I'm using the following code to display WebView in my android app

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

If history exists instead of just exiting WebView, I need to enable the back button to go back

I've tried many different code examples such as this The application will not close until the back button is pressed

This is my code and the back button code, but it just crashes the application when the back button is pressed:

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

Can someone help me solve it?

Solution

Got it! Your problems in this business

WebView webview = new WebView(this);

You are creating variables inside the function instead of using your member variables, so your member variables are null in the onkeydown function

Just replace it

webview = new WebView(this);
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>