android webview history does not work properly - android

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?

Related

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

Android application does not relaunch when navigating to the home screen

We have a launcher application that works fine on older versions of Android. We have a device that is running Android 5.1, and are running into issues.
When pressing the back button from within the application, we allow the user to go to the settings page. Pressing the home key re-launches the application. Pressing the back button on other devices also relaunches the application.
On the new device, pressing the back button allows us to navigate to the Android home page. It does not launch the application.
We are overriding the back button like so:
#override
public void onBackPressed() {
// Display the password prompt if required
if (PreferencesManager.isPasswordPresent()) {
LeaveApplicationPasswordDialogFragment dialog = LeaveApplicationPasswordDialogFragment.getInstance();
dialog.show(getSupportFragmentManager(), "password");
}
else {
// Prompt whether we are about to leave the app
LeaveApplicationDialogFragment dialog = null;
MyApplication application = (MyApplication )
getApplication();
if (application.isDefaultLauncher()) {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ** to access the device's settings?");
}
else {
dialog = LeaveApplicationDialogFragment.getInstance("Are you sure you want to leave ***");
}
dialog.show(getFragmentManager(), "leaving");
}
}
In the dialog fragment, we accept the confirmation and process it like so:
public void exitToSettings() {
GUIAndroidTouchBaseActivity.this.startActivity(new Intent(android.provider.Settings.ACTION_SETTINGS));
shutdownOperations();
finish();
}
Per some research and other threads, I worked with our exit method like so:
public void exitToSettings() {
Intent intent = new Intent(android.provider.Settings.ACTION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP );
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
GUIAndroidTouchBaseActivity.this.startActivity(intent);
shutdownOperations();
finish();
}
No dice. Same behavior.
What am I missing? Is there something in OS 5.1 that's overriding our launcher? Again, pressing the home button launches the app as expected. Navigating to the home page from the settings page by pressing the back button does not.
What we have works on other devices and OSs. We've had no issue with 4.1 and 6.1.
We are also overriding the back button like so:
#Override
public boolean onKeyDown(int keyCode, KeyEvent KEvent) {
int deviceid = KEvent.getDeviceId();
//Making sure not processing same key again
if (KEvent.getRepeatCount() != 0) {
return true;
}
if (!SettingsOpened) {
int keyaction = KEvent.getAction();
// "Esc" key can not be stooped id diveceid is non zero because it can be back key of android
if (KEvent.getKeyCode() == KeyEvent.KEYCODE_BACK && deviceid != 0) {
return super.onKeyDown(keyCode, KEvent);
}
if (keyaction == KeyEvent.ACTION_DOWN) {
String key = KeyEvent.keyCodeToString(keyCode); //wont work in version 11 or less
if (keyCode != KeyEvent.KEYCODE_ENVELOPE) {
Matcher matcher = KEYCODE_PATTERN.matcher(key);
if (matcher.matches() || ExternalKeyboard.keyMatches(KEvent)) {
int keyunicode = KEvent.getUnicodeChar(KEvent.getMetaState());
char character = (char) keyunicode;
//toast.makeText(this, "onKeyDown" + _lastChar + repeatcount, toast.LENGTH_SHORT).show();
_lastChar = character;
_actionDown = true;
ExternalKeyboard.KeyboardAddChar(character);
}
}
}
return true;
}
else {
return super.onKeyDown(keyCode, KEvent);
}
}
Thanks!
Adding
android:stateNotNeeded="true"
android:clearTaskOnLaunch="false"
to my manifest took care of it.

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

In webview, tapping link opens link in new activity but still loads it in the first activity, too

I tried including the relevant bit. I'm new at this and working with existing code. I hope someone can point me in the right direction.
Basically, say my website is www.mywebsite.com (lets call this 'A' since I can only post two hyperlinks) and you decide to do a search, so now the URL looks like this:
www.mywebsite.com/?s=notes (call it 'B')
You then get the SearchPage activity with the search results. Now say I tap a link from the search results. Example:
the url above/category/easy-news/ (call it 'C')
Website 'C' should load in PageActivity (which it does) but still loads up in SearchPageActivity which was initially showing 'B'.
On top of all of that, there seems to be a 3rd PageActivity (though it doesn't look like it opens a third time). One that also loads 'C' again. As mentioned, it is still the PageActivity, though. Pressing back closes the PageActivity and shows another PageActicity with the URL 'C'.
Closing that a second time takes me back to SearchPageActivity with the same URL 'C' which was initially 'B'. Pressing back one more time takes me back to 'B' within the same activity.
I don't seem to have this issue with the MainActivity, only in the SearchPage activity. In the MainActivity, things work fine. The code below is from SearchPage activity.
Sorry for the long winded message. I hope someone can help. Thanks.
Thanks.
private boolean isAppOrGamePage(String paramString) {
if ((paramString == null)
|| paramString.length() < 24
|| (!paramString.substring(0, 24).equals(
"http://www.mywebsite.com")))
return false;
String str1 = paramString.substring(24);
String str2 = str1.substring(0, 1 + str1.indexOf('/', 2));
if ((!mAppIdentifiers.contains(str2))
&& (!mGameIdentifiers.contains(str2)))
return false;
return true;
}
public boolean shouldOverrideUrlLoading(WebView paramWebView,
String paramString) {
if (MainActivity.DEBUG)
Log.e("shouldOverride", paramString);
if (Uri.parse(paramString).getHost() != null
&& (!Uri.parse(paramString).getHost()
.equals("myspace.com"))
&& (!paramString.contains("facebook.com"))
&& (!Uri.parse(paramString).getHost()
.contains("twitter.com"))
&& (!Uri.parse(paramString).getHost()
.equals("goo.gl"))
&& (!Uri.parse(paramString).getHost().contains("bit.ly"))
&& (!Uri.parse(paramString).getHost()
.contains("plus.google.com"))
&& (!Uri.parse(paramString).getHost()
.contains("youtube.com")))
if (isAppOrGamePage(paramString)) {
final Intent intent = new Intent(SearchActivity.this,
PageActivity.class);
intent.putExtra("app_url", paramString);
startActivity(intent);
} else
return false;
return false;
}
}
Return true when you open the other activity. You need to consume that call.
Returning true tells the webview that you have handled it, and don't want it to do anything with it.

Webview Strange behaviour

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..

Categories

Resources