6

I am trying to override the back button so that it goes back the previous page but it just exits the application. Here;s the code.. please tell me where am i going wrong.

package com.example.thebase;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainAccelerometer extends Activity implements AccelerometerListener{

WebView myWebView;
//Back Button Code
     @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN){
                switch(keyCode)
                {
                case KeyEvent.KEYCODE_BACK:
                    if(myWebView.canGoBack() == true){
                        myWebView.goBack();
                    }else{
                        finish();
                    }
                    return true;
                }

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


//--------------------  


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.accelerometer_example_main);
    //ENABLING JAVASCRIPT CODE
            WebView myWebView = (WebView) findViewById(R.id.webview);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);


            //WEBVIEW CODE
              myWebView = (WebView) findViewById(R.id.webview);
            myWebView.loadUrl("http://www.pingcampus.com/greedy/get_category.php");


            //WebView client
              myWebView.setWebViewClient(new HelloWebViewClient());

              //Back Button

              //------


        };
//rest of the code
Arihant
  • 3,847
  • 16
  • 55
  • 86

2 Answers2

23

You have onBackPressed() method to override instead of onKeyDown(). Replace onKeyDown() with onBackPressed() as below:

@Override
public void onBackPressed() {
    if (myWebView.canGoBack()) 
        myWebView.goBack();
     else 
        super.onBackPressed();

}

EDIT

Also check 3rd line in your onCreate()

WebView myWebView = (WebView) findViewById(R.id.webview);

You are declaring myWebView as local variable again and global variable is not instantiated which is throwing NullPointerException and crashes the app.

So change the your 3rd line of code as below:

myWebView = (WebView) findViewById(R.id.webview);
Naveed Ahmad
  • 6,627
  • 2
  • 58
  • 83
TNR
  • 5,839
  • 3
  • 33
  • 62
  • @TNR Should the above code block come withing the onCreate or outside of it? – Arihant Nov 25 '13 at 09:58
  • @Arihant outside of onCreate() – TNR Nov 25 '13 at 09:59
  • I did paste it outside the onCreate but as i press the back button the app just gives error saying "Unfortunately this has stopped" – Arihant Nov 25 '13 at 10:00
  • thanks! this is what saved my day: "You are declaring myWebView as local variable again and global variable is not instantiated which is throwing NullPointerException and crashes the app." – Kilise Nov 22 '14 at 10:26
  • Thank you for saying this: "You are declaring myWebView as local variable again and global variable is not instantiated which is throwing NullPointerException and crashes the app."! – Jawaad Jul 08 '15 at 13:43
2

No need to check for ACTION_DOWN. Just:

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

or alternatively, instead of onKeyDown() you can override onBackPressed():

@Override
public void onBackPressed() {
    if( mWebView != null ) {
        if( mWebView.canGoBack() ) {
            mWebView.goBack();
            return;
        }
    }

    return super.onBackPressed();
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141