Dave M
Dave M

Reputation: 53

Need assistance using phones back button to go back in Webview

This is my code:

package com.testappmobile;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class testappmobileActivity extends Activity

{

    final Activity activity = this;

    private WebView webview;


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the BACK key and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            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);
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);

        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });


        webView.loadUrl("http://developer.android.com/index.html");
    }
}

Now im having trouble getting the hardware back button to work. The app loads fine as does page and everything else but as soon as i hit the phones back button it crashes then forces close. I've searched google for the last 3 hours and have only found vague answers with very little info or broken links. Google's instructions sucked also as i am a beginner and they presume you know a certain amount.

Where should I place the code if it is in the wrong place?

Are there errors?

Cheers!

Upvotes: 5

Views: 926

Answers (2)

Mudassir
Mudassir

Reputation: 13184

You have created a webView reference at class level but never initialized it thats why NullPointerException. I've made a small change in the onCreate method in your code, analyze it and make the necessary change(s);

@Override
public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
                setContentView(R.layout.main);

                // Don't create another webview reference here,
                // just use the one you declared at class level.
                webView = (WebView) findViewById(R.id.webView);
                webView.getSettings().setJavaScriptEnabled(true);

                webView.setWebChromeClient(new WebChromeClient() {
                    public void onProgressChanged(WebView view, int progress)
                    {
                        activity.setTitle("Loading...");
                        activity.setProgress(progress * 100);

                        if(progress == 100)
                            activity.setTitle(R.string.app_name);
                    }
                });

          // rest of code same
          // ...
          // ...
          // ...
}

Hope you got it. :)

Upvotes: 4

Salw
Salw

Reputation: 1900

You have two different references to WebView. First local in onCreate, that is lost. Second webview member that you use in onKeyDown, but that is null all the time.

Upvotes: 0

Related Questions