Webview Strange behaviour - android

I am developing an android app, here i am little confused, firstly, i a loaded a html form data in webview. then navigated to different other pages from the links in my first page. Now coming back to the first page i have use the following code in "onBackPressed()" method. but it shows me empty webview on first page. to avoid that i added another if condition which is written below, but result is still the same,
if(webview.canGoBack() == true)
{
webview.goBack();
if(!webview.canGoBack())
webview.loadDataWithBaseURL("http://xyz.com/feed/", data, "text/html", "utf-8", null);
}
// if now on fist page then do the following
else if(activityno==2) {
Intent fav = new Intent(Views.this, FavroiteShow.class);
startActivity(fav);
}
//on first page and activity no is not 2 then do the following
else
finish();
}
its like, if webview can go back,simply goback and do nothing else. else if webview cannot go back then check for activity no and go for the code in it, else finish. i want to add one thing to it that if webview has come to the first page. then load my data no matter what the activity no is.
where am i wrong.
Thanks

Try now..
if(webview.canGoBack() == true)
webview.goBack();
else //if there is no history available
webview.loadDataWithBaseURL("http://xyz.com/feed/", data, "text/html", "utf-8", null);

#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
} else if (!webview.canGoBack()) {
webview.loadDataWithBaseURL("http://xyz.com/feed/", data,
"text/html", "utf-8", null);
// if now on fist page then do the following
if (activityno == 2) {
Intent fav = new Intent(Views.this, FavroiteShow.class);
startActivity(fav);
}
else {
finish();
}
}
return super.onKeyDown(keyCode, event);
}
I have used overriden Class onKeyDown. and Checkes here the KeyEvent.
Hope this Works for you..

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");
}

Previous page in Android Studio Webview

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);
}

android webview history does not work properly

My application loads a url of a website into an android webview.
This website consists of 3 pages (actually its the same page with different ID)
lets call them A B & C.
The website, has a back button, which code is:
javascript:history.go(-1)
If i browse from A to B and presses the back button - it works fine.
If I browse from B to C and presses the back button - I get back to B - also fine.
If I then press the back button, I get back to C instead of getting to A - NOT fine.
Then If i press back I will get back to B...back again to C...and then to B...endlessly, I never get to A.
If I use the physical back of the device and use the following, it works as expected:
if ((keyCode == KeyEvent.KEYCODE_BACK) && webView.canGoBack()) {
webView.goBack();
return true;
}
Extra info:
I am using a WebViewClient, override "shouldOverrideUrlLoading" and enable javascript:
webView.getSettings().setJavaScriptEnabled(true);
Any ideas?
Thanks!
I write code like this
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.startsWith("tel:") || url.startsWith("mailto:")) {
return true;
}
if (mOnWebViewEventListener != null) {
mOnWebViewEventListener.onWebViewPageStart(view);
}
if (mShowProgress) {
mProgressBar.show();
}
return false;
}
It's work or not?

Android - handling the back button

I have an app which requires the user to be logged in. In the app, to be logged in is to contain a specific string in the SharedPreferences file.
It is totally working except for one use case: when the user presses the phone's back button.
Is there something different that happens in loading the page when the user presses the phone's back button that I should be aware of and account for in the code?
Thanks!!
#Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_BACK)
{
//do your stuff here
return true;
}
else
return super.onKeyUp(keyCode, event);
}
try the above code to handle back key event

Categories

Resources