0

In my android webview I want app to exit if back button is pressed only when user is on home page, but on any other page the back button should work normally and go back to previous session. I have build one php application which I'm showing to my users through android webview. Now if user opens the app, it will open the home page of my php application from where users can navigate through various option and then come back to home page, now here when users come to home page and press back button its going to previous page which I dont want. I want something like if the user is on home page of the app and press back button, it should just exit the app.

This is what I've tried for goback:

@Override public void onBackPressed() {
    if (webView.canGoBack()) { 
        webView.goBack(); 
    } else { 
        super.onBackPressed(); 
    } 
}
WurmD
  • 1,231
  • 5
  • 21
  • 42
  • Welcome to SO. Please post the code you already have. – Alexander Dobernig Jan 27 '21 at 08:54
  • i have below code for goback: @Override public void onBackPressed() { if (webView.canGoBack()) { webView.goBack(); } else { super.onBackPressed(); } } – Pluto Webs Jan 27 '21 at 09:47
  • Hi Pluto, Alexander meant "edit into your question the code your already have" :) Also, what happens with that code, it doesn't work to your specifications? – WurmD Jan 27 '21 at 15:41
  • I have added the code to my question. And this code works fine as it takes user to previous page if back button is pressed in webview, but what i want is that if back button is pressed on home page it should exit the app but if back button is pressed anywhere else in webview, it should just go back to previous page as how it is working now. – Pluto Webs Jan 28 '21 at 07:29

0 Answers0