WebViewClient history wrong on HoneyComb - android

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

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

Android Google Keyboard crashes when capturing KeyEvent.KEYCODE_ENTER

I'm trying to manage the Next event on the Google Keyboard on a Google Nexus 5. I want my application to check user information when the Next button gets pressed.
The code looks like this:
private TextView.OnEditorActionListener GenerateEditorListeners()
{
return new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId == EditorInfo.IME_NULL && event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
};
}
On a Samsung Galaxy S4 works perfect, but on a Google Nexus 5 the actionId = 0 and the event = null. So I figure out that the Samsung keyboard works fine with this code, but doesn't happen the same with the Google Keyboard.
Any idea on why it's not wokring for Google's keyboards?
EDIT: I've read in this post that Google keyboard has a bug in some LatinIME keyboards.... I'm using a latin one. If that's the problem, how to solve it?
I could solve it by using the OnKeyListener event:
textUserEmail.setOnKeyListener(new OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(event.getKeyCode() == KeyEvent.KEYCODE_ENTER){
if (!finished)
{
if (CheckUserInfo()) finished = true;
}
return true;
}
else
{
return false;
}
}
});
Now it works in all keyboards.

Android - Disable Device Back button

I am working on android application using PhoneGap. I need to handle Device back button functionality by using the below code:
import com.phonegap.DroidGap;
public Class MyClass extends DroidGap {
appView.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP) {
finish();
return true;
}
return onKeyDown(keyCode, event);
}
});
}
By using the above code, Application getting exited because i have used finish(); But i want nothing should be happened on click of Device back button. How can i acheive that? Please help me.
Why do you need to do this at the Java level? You can achieve this with Javascript using Phonegap's Event API
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown(e) {
e.preventDefault();
}

Handling back request properly for an app to work as far back as SDK 4 (API Level 1.6)

I've noticed that the 'back' request used to be made at the point a user pressed down on the 'back' key but at some point this was changed so that a 'back' request is made instead at the point the 'back' key is released. (Correct me if I am wrong!) Does anyone know from which SDK (or API Level) exactly this change was made effective? I think it was SDK 2.0 (API Level 5) and hence have the code in my Activity as follows but would like to be certain...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR)
handleBackRequest();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public boolean onKeyUp(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK)
{
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ECLAIR)
handleBackRequest();
return true;
}
return super.onKeyUp(keyCode, event);
}
Use onBackPressed() for Android 2.0 and higher. Use onKeyDown() for Android 1.6.
With the code in my previous post I was getting some unwanted behaviour (when dialogs, the android keyboard etc were showing up) and on CommonsWare's advice, changed my code to the following and it seems to be working alright for the different SDKs...
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK
&& android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.ECLAIR)
{
onBackPressed();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onBackPressed()
{
// handle back request here
}
... Let me know please if there's anything here not quite right!

Backwards compatibility of onBackPressed

If I use onBackPressed() on Android 1.5, my application crashes. Is there any possibility to deactivate this function if running on an Android 1.5 device?
The code there is not absolute necessary but a real "nice to have", so I would like to keep it on newer devices and just drop it on older ones.
Is this possible?
edit: I think I found it, just the old way:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
// do something on back.
return true;
}
return super.onKeyDown(keyCode, event);
}
You can try to detect runtime which version of SDK using your application and depending on that prepare different branches. Like:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if(Build.VERSION.SDK_INT==Build.VERSION_CODES.CUPCAKE) //if it's 1.5
{
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0)
{ // do something on back.
return true;
}
}
return super.onKeyDown(keyCode, event);
}

Categories

Resources