How to Go Back to Previous Page If Back Button Is Pressed in Webview

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

I use something like this in my activities with WebViews:

@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();
} else {
finish();
}
return true;
}

}
return super.onKeyDown(keyCode, event);
}

Edit:

For this code to work, you need to add a field to the Activity containing the WebView:

private WebView mWebView;

Initialize it in the onCreate() method and you should be good to go.

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

Android Webview make back button go to previous page

I think you should override your activity OnBackPressed :

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

Android - WebView back button

Override the onBackPressed method in your activity as below:

@Override
public void onBackPressed(){
WebView wv = (WebView)findViewById(R.id.webview1);
if(wv.canGoBack()){
wv.goBack();
} else {
super.onBackPressed();
}
}

Use the above method inside your class, not outside.

public class MainActivity extends AppCompatActivity {

//onCreate method

//onBackPressed method

//other methods

}

How to make device back button to go to the previous page in flutter webview

Wrap Scaffold with this widget

WillPopScope(
onWillPop: controller.onBackPressedHandler,
child: Scaffold( ... )

Future<bool> onBackPressedHandler() {

return Future.value(false);} //return false disables default functionality

you can control back button functionality in onBackPressedHandler function.
For your case you should put this line of code inside it

webviewController.goBack();

Back when back button is pressed in webview

This is working for me.

@Override
public void onBackPressed() {
WebView webView = findViewById(R.id.webView);
if (webView.canGoBack()) {
webView.goBack();
}
else {
......
}
}

How to detect back button click and make webview go back

you need to make browser a class level variable so you can access it anywhere in your page.

public class WebPage : ContentPage
{
Xamarin.Forms.Webview browser;

protected override bool OnBackButtonPressed()
{

base.OnBackButtonPressed();

if (browser.CanGoBack)
{
browser.GoBack();
return true;
}
else
{
base.OnBackButtonPressed();
return true;
}

}

public WebPage()
{


browser = new Xamarin.Forms.WebView();

browser.Source = "https://myurl.com";


Content = browser;

}
}

WebView - only show back button if back history exists

It works like this:

blizzView.setWebViewClient(new WebViewClient() {      //
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);

backButton = findViewById(R.id.backButton);
backButton.setEnabled(blizzView.canGoBack());

if (blizzView.canGoBack()) {
backButton.setVisibility(View.VISIBLE);
} else {
backButton.setVisibility(View.INVISIBLE);
}

}
});


Related Topics



Leave a reply



Submit