0

I created a basic WebView app, want to add a back button function which would take the user to the previous page rather than exiting the app. Have attached the MainActivity.java code with this question below.I am a newbie to stack overflow and android development.

package example.com;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

    public class MainActivity extends AppCompatActivity {

        private WebView webview;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        webview.setWebViewClient(new WebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setDomStorageEnabled(true);
        webview.setOverScrollMode(WebView.OVER_SCROLL_NEVER);
        webview.loadUrl("https://www.example.com");

        }
}
Android
  • 1,420
  • 4
  • 13
  • 23
  • Have you tried this, https://stackoverflow.com/questions/37673558/android-webview-make-back-button-go-to-previous-page/37673643 – Android Jun 17 '19 at 12:39

2 Answers2

1

@Override onBackPress() inside your mainActivity and add your navigation logic inside it. Here is working snipped

public class MainActivity extends AppCompatActivity { 

    private WebView webView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        webView = findViewById(R.id.webView);
        webView = new WebView(MainActivity.this);
        webView.loadUrl(...);

    }

    @Override
    public void onBackPressed() {
        if (webView != null && webView.canGoBack()) {
           webView.goBack();
        }else{
           //Your App exit logic here
        }
    }

}

I hope this will meet your requirements.

Vipul Prajapati
  • 1,183
  • 9
  • 11
  • Your code is incorrect, I noticed you're calling `setContentView` twice which can't ever be a good idea. You can simply keep the first one, and remove the whole `webView = new WebView(MainActivity.this);` – Zun Jun 17 '19 at 12:42
  • Thanks for providing me the logic. – Nova_Prime Jun 17 '19 at 13:06
  • It's still wrong, you're unnecessarily creating a new WebView object. Did you test this code? – Zun Jun 18 '19 at 07:15
  • how to make the back button exit the app by double tapping it? – Nova_Prime Jun 23 '19 at 11:19
  • @Nova_Prime check out [here](https://stackoverflow.com/a/13578600/10835287). that was working solution for me. Remember your app exit code!, you need to add inside `else` on `onBackPressed()`. – Vipul Prajapati Jun 24 '19 at 04:02
  • I used this long back_pressed; @Override public void onBackPressed() { if (back_pressed + 1000 > System.currentTimeMillis()){ super.onBackPressed(); } else{ Toast.makeText(getBaseContext(), "Press once again to exit!", Toast.LENGTH_SHORT) .show(); } back_pressed = System.currentTimeMillis(); } But the app is not exiting after double tapping back button,the toast is showing up. – Nova_Prime Jun 24 '19 at 11:58
0

Try this, hope this helps

   @Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) {
                    mWebView.goBack();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}
JDevoloper
  • 227
  • 4
  • 17
  • Why call `finish()` instead of calling the `onKeyDown` from the super class? Just because you want to close the activity doesn't mean you should call `finish` – Zun Jun 18 '19 at 07:16