2

I have an android app with WebView but when i press the hardware back button, nothing happens. I want the user to be taken back to the previous page loaded on the Webview. What am i missing in this code:

package kenya.cancerbank;

import tscolari.mobile_sample.R;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class CancerBank extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WebView mainWebView = (WebView) findViewById(R.id.mainWebView);

        WebSettings webSettings = mainWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);

        mainWebView.setWebViewClient(new MyCustomWebViewClient());
        mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);

        mainWebView.loadUrl("http://kenyacancerbank.org/android");
    }
    @Override
    public void onBackPressed() {
    return;
    }

    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }
}

What do i need to add to this code to return to the previous page they had visited when they press the hardware back key.

}
    @Override
    public void onBackPressed() {
    return;
    }
  • This thread has an answer for you. http://stackoverflow.com/questions/3141996/android-how-to-override-the-back-button-so-it-doesnt-finish-my-activity – Mikael Engver Mar 16 '13 at 00:49

3 Answers3

1

You will need to manually make the WebView go back a page. Simply returning just gives you a useless method since it doesn't do anything else but return. And since you overrode the superclass method, you are seeing a "non working button"

Make WebView mainWebView an instance variable:

public class CancerBank extends Activity {
   WebView mainWebView;
   //rest of code

Then find in onCreate() assign it the proper reference:

 @Override
 public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      mainWebView = (WebView) findViewById(R.id.mainWebView);
      //rest of code

Then override onBackPressed() and check if the WebView can go back. If it can, make it go back via goBack():

@Override
public void onBackPressed() {
  if (mainWebView.isFocused() && mainWebView.canGoBack()) {
            mainWebView.goBack();       
    }
}
Community
  • 1
  • 1
A--C
  • 36,351
  • 10
  • 106
  • 92
  • 1
    I have tried your code but it actually exits the application. what can i do to return back to the previous page? – Kelvin Maithya Mar 16 '13 at 01:06
  • 1
    @KelvinMaithya did you load more than one page? Right now, your app shouldn't actually exit anything. I purposefully omitted the `finish()` call so you can't exit this Activity. – A--C Mar 16 '13 at 01:07
  • nope...just navigated 1 page away but still exiting....what should the exact code look like – Kelvin Maithya Mar 16 '13 at 01:15
  • @KelvinMaithya As far as I know, this is what it should look like. If the `webView` can go back it will and if it can't nothing should happen. Did you copy my `onBackPressed()` method exactly as I wrote it (with the `@Override`)? – A--C Mar 16 '13 at 01:19
0

You do not have to override onBackPressed() at all, just let the OS handle this for you. If you put the super.onBackPressed(); in your onBackPressed() method. You would get the right behavior. But again you do not have to do anything.

Hoan Nguyen
  • 18,033
  • 3
  • 50
  • 54
0

you can use the onKeyDown(int keyCode, KeyEvent event)
if( keyCode == KeyEvent.KEYCODE_BACK)
method to detect the hardware back key press and do watever step you like to do either exit or go one step back

krishna
  • 142
  • 7
  • 12