Back button on webview

Asked

Viewed 735 times

0

I’m starting in the area of android app development. So I started to develop an application which I created 4 layouts ( Main screen, central subscriber, contact and plans). In the subscriber’s central Activity I put a webview and when the client presses the back button can navigate the pages with this code I put in the java of Activity:

Override
public void onBackPressed(){

if (mywebView.canGoBack()){

mywebView.goBack();

} else {

} 

Only when the client navigates inside the webview and goes back to the main web page through the mobile button, it stops at this page and does not return to the menu.

I tried to create a back button with startactivity in the header to return to the menu, but gives error saying "the application stopped".

Thanks in advance for the help!

1 answer

1

If you are not finishing the main activity by starting the subscriber’s central activity, you can simply end this second activity to return to the first.

Example:

Override
public void onBackPressed(){
    if (mywebView.canGoBack()){
        mywebView.goBack();
    } else {
        finish();
    }
}
  • Perfect! Thank you very much. It worked!

  • If it’s not too much! I’d like to put a button in the header for the main menu. I declared startactivity in main.java and called on the button inside Activity with android:onclick:"startactivity". Is giving error. There’s another way to do it?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.