Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Back button in a webview in Android

When using the normal mobile browser, when you press the Back button, it takes you to the previous page. When a WebView is used in my app to display a web page and the user wants to return to the previous page, how is typically done? Does pressing the Back button automatically take the user back to the previous page or does it take them back to the previous Activity? I would assume that since the Back button is normally meant to take the user to the previous activity, it shouldn't be used to return to a previous web page. If this is the case, should my mobile web page include its own Back link that a user clicks on?

I guess what I am trying to do is to understand the correct behavior I should be employing.

like image 564
Johann Avatar asked Dec 01 '25 06:12

Johann


2 Answers

Let's look at some popular browser. currently, in my device, there are two browsers

  1. chrome
  2. default internet browser

In both browsers, it goes to the previous page. So it is a common behavior. to do so

you can overwrite the onBackPressed to go to your previous page.

@Override
public void onBackPressed(){
            if(mWebView.canGoBack() == true){
                mWebView.goBack();
            }
            else{
                finish();
            }
}
like image 149
stinepike Avatar answered Dec 03 '25 20:12

stinepike


Please keep in mind that back buttons is coming with majority of the android devices. So you do not need a back button in your activity to go back to previous activity.

And about the webview if you want navigation in the webview to travel all the previous pages then you can put a back button and implement the navigation of the webview using the method webview.goBack().

In short, If you want your user to navigate to previous page then you should give functionality of the webview to go back to previous page and from the first page user click on back button again finish the activity.

like image 30
Dharmendra Avatar answered Dec 03 '25 19:12

Dharmendra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!