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
How to Check Visibility of Software Keyboard in Android
Android "Only the Original Thread That Created a View Hierarchy Can Touch Its Views."
Detect Whether There Is an Internet Connection Available on Android
How to Make an Imageview With Rounded Corners
All Com.Android.Support Libraries Must Use the Exact Same Version Specification
Activity Has Leaked Window That Was Originally Added
Static Way to Get 'Context' in Android
Android Studio: Add Jar as Library
What Does Android:Layout_Weight Mean
How to Get a List of Installed Android Applications and Pick One to Run
"Cannot Resolve Symbol R" in Android Studio
How to Stop Edittext from Gaining Focus When an Activity Starts in Android
How to Get the Source Code from an Apk File
How to Make My Custom Objects Parcelable
How to Get the Build/Version Number of Your Android Application