2

I'm developing a Javascript based web app and thought that I could use Android's WebView ability to make it like an actual app, so I jumped right into Android Studio and made a simple WebView app without any Java knowledge, but the problem is, between page transitions whenever I want to return back and hit the back button, the app closes itself. I found some solutions but I have no idea how to implement them correctly, so can you help me please? Here are my WebView's FullscreenActivity.java codes.

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    WebView view = (WebView) this.findViewById(R.id.webView);
    view.getSettings().setJavaScriptEnabled(true);
    view.setWebViewClient(new ZeroBrowser());
    view.loadUrl("http://www.google.com");
}

private class ZeroBrowserOverride extends WebViewClient implements ZeroBrowserOverride {

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

Thank you so much!

dguay
  • 1,635
  • 15
  • 24
Ozan
  • 85
  • 6
  • 1
    please go here http://stackoverflow.com/questions/6077141/how-to-go-back-to-previous-page-if-back-button-is-pressed-in-webview/9743841#9743841 – Emil Nov 05 '15 at 14:12
  • @Boss Thank you so much, but actually I've already read all those answers, yet I couldn't implement any of them since I have no Java experience... I was just hoping to have a deeper help, actually. – Ozan Nov 05 '15 at 14:30

3 Answers3

2

You can try with this .

WebView view ; //Global    

  @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        view = (WebView) this.findViewById(R.id.webView);
                view.getSettings().setJavaScriptEnabled(true);
        view.setWebViewClient(new ZeroBrowser());
        view.loadUrl("http://www.google.com");
    }

Then

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

How to go back to previous page if back button is pressed in WebView?

http://developer.android.com/intl/es/guide/webapps/webview.html

Community
  • 1
  • 1
IntelliJ Amiya
  • 74,896
  • 15
  • 165
  • 198
  • 1
    It really worked! Could you please explain what did you do up there? Thank you so much! :) – Ozan Nov 05 '15 at 14:41
1

You can check the clients history to go back within webview. Implement something like this:

@Override
public void onBackPressed() {
  if (mWebView.canGoBack()) {
      mWebView.goBack();
  } else {
      super.onBackPressed();
  }
}
Thomas R.
  • 7,988
  • 3
  • 30
  • 39
1

In your WebView class (or in parent view) override the listener:

@Override
public boolean onKeyDown(final int keyCode, final KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && this.canGoBack()) {
        this.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);
}
Rafouille
  • 863
  • 10
  • 16
  • I tried to add the lines into my WebView class, but I think it couldn't recognize the functions over there.. I know I sound so noobish, but as I said I have no Java experience so far... "Error:(14, 55) error: cannot find symbol method canGoBack()" I have this kind of errors right now. – Ozan Nov 05 '15 at 14:28
  • Is your class extends WebView aka public class MyWebView extends WebView ? What if you put this override in you main activity? – Rafouille Nov 05 '15 at 14:31
  • Actually it was WebViewClient, and I think that was the problem! The other answer above solved my problem, thank you for all your help! – Ozan Nov 05 '15 at 14:39