Previous page in Android Studio Webview - android

Currently I'm actually making a mobile app for my website (wordpress) using Webview in Android Studio.
But while navigating into the different pages of it, when I want to go to the previous page using the back button of my android device, my app just close.
How can I solve this?

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

To implement Back Navigation for WebViews, As from documentation, you can override onBackPressed() and and proxy to the WebView if it has history state as:
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
return;
}
// Otherwise defer to system default behavior.
super.onBackPressed();
}
Or, you can check the event was Back button and if there's history, then you can also use:
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
// If it wasn't the Back key or there's no web page history, show default behavior
// system behavior (probably exit the activity)
return super.onKeyDown(keyCode, event);
}

Related

How to disable the back button on android with Apache Cordova?

sorry for any spelling mistakes as english is not my first language.
So, to the question:
I am making an android app using jquery mobile and Apache Cordova, both in the latest versions, so the question is:
Is there a way i can disable the back button on the android phone when in the app?
Add this to your MainActivity:
#Override
public void onBackPressed() {
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// appView.loadUrl("javascript: clickBack()");
return true;
}
return super.onKeyDown(keyCode, event);
}
you can handle backbutton like this
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
so its not going to close whatever you are going to do i think

how to close a webview which was opened by javascript in android programming

I have an android application which contains only one web view , I have loaded the url(which was developed by .net using javascript)in web view , It has facebook login , when user click on that facebook button,it opens the facebook dialog,if user don't want to login,he will click on go back on android but that facebook dialog still appears,how to go back fom facebook dialog to main web view?
I tried this code
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mainWebView.canGoBack()) {
mainWebView.goBack();
Log.d("webview<---", "going_back");
} else if (newWebView != null && newWebView.isFocusable()) {
FBWebView.removeAllViews();
FBWebView.clearHistory();
FBWebView.getParent();
FBWebView=null;
//FBWebView.addView(mWebView);
// FBWebView.loadUrl(url);
//startActivity(new Intent(MainActivity.this, MainActivity.class).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
Log.d("fb_webview<---", "finish");
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
still no use,Can Anyone suggest that how to go back from fb dialog to main web view ,here FBWebview is created dynamically in this activity and mainWebview is intialised through xml , how to close fbWebView to appear the mainwebview?
Write below code in before return statement under KeyEvent.KEYCODE_BACK case:
/* Closing newly created popup window */
if (Build.VERSION.SDK_INT < 18) {
newWebView.clearView();
} else {
newWebView.loadUrl("about:blank");
}

Intercept Android system keyboard shortcuts to override functionality

Is it possible for an Android application to intercept external keyboard shortcuts (e.g. Alt+Tab) and action them before the OS performs the default behaviour? Using Alt+Tab as an example, I would like to be able to have my app respond to Alt+Tab within my application and not have Android switch applications to a different app.
I have tried searching, but have not been able to find an answer, I think my Google-Fu is failing me!
It maybe is not as extense or detailed as it would be needed for capturing a shortcut, but you may want to override the onKeyDown event in your activity.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
Log.d(this.getClass().getName(), "Back button pressed");
}
if ((keyCode == KeyEvent.KEYCODE_HOME)) {
Log.d(this.getClass().getName(), "Home button pressed");
}
if ((keyCode == KeyEvent.KEYCODE_MENU)) {
Log.d(this.getClass().getName(), "Menu button pressed");
}
//return true;
return super.onKeyDown(keyCode, event);
}

Fascinate Device Web view go back() method Issue

I am developing an app in android. In the app i am using a web view and i am saving the web view history.When user clicks on device back button i am loading the previous url(using web view goback() method) if web view history is not empty.It is working fine in all the devices except the Samsung Fascinate device. In this device web view goback() method is not working.
Any suggestions??
Thanks,
Ram.
Finally managed to figure out how to do it
#override
public void onFormResubmission(WebView view, Message dontResend, Message resend)
{
resend.sendToTarget();
}
default behaviour of onFormResubmission is not to resubmit. resend.sendToTarget(); changes that.
this will make ur hardware back button work if you have made some code to handle the hardware button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
mWebView.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}

WebViewClient history wrong on HoneyComb

I have a WebViewClient in an Activity in which I override the onKeyDown method like this
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
finish();
return true;
}
}
This works like a charm on my phone as well as the emulators I tested on including a 3.0 emulator.
Weird thing is that on a 3.1 emulator as well as on my Xoom tablet (3.0.1) it does NOT work. It seems that webView.canGoBack() always returns true on these platforms.
Questions:
Has anybody else found similar behaviour?
Do you have a workaround/hack that allows me to make the backbutton work to navigate in the web view history as well as ultimately out of the activity if required?
Update: I have since then change the app to use fragments with the compatbility library so I am now using this:
webView.setOnKeyListener(
new View.OnKeyListener() {
#Override
public boolean onKey(View view, int keyCode, KeyEvent keyEvent) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
} else {
redirectHelper.finish();
return true;
}
}
}
);
where redirect helper basically is a wrapper for proper finishing of an activity or removing a fragment from the stack. Still has the same issue though..
I'm using this without issues on 3.1 and Galaxy Tab 10.1. Haven't tried onKeyDown method.
#Override
public void onBackPressed() {
if( webView.canGoBack() ) {
webView.goBack();
} else {
super.onBackPressed();
}
}

Categories

Resources