Android - Disable Device Back button - android

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

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

onKeyDown - Control key is not working in Activity

I am trying to add shortcuts in my app when external keyboard is connected. SHIFT+KEY, this combination is triggering the following event. But Control+KEY and Control key events are not triggered. Can anyone suggest me on this.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_CTRL_LEFT) {
flag_sellreceipt = KeyEvent.isModifierKey(KeyEvent.KEYCODE_CTRL_LEFT);
int i = KeyEvent.META_CTRL_LEFT_ON;
System.out.println("ctrl is pressed");
}
return
super.onKeyDown(keyCode, event);
}
Note: I have tested this in emulator.

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!

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

onClick HOME key display alert box in android

I am sincerely trying to display alert box onClick of Home key, but I am not succeeded. So please help me to write the code otherwise, show me the code.
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (keyCode == KeyEvent.KEYCODE_BACK )
{
finish();
return false;
}
else if (keyCode == KeyEvent.KEYCODE_HOME)
{
finish();
return false;
}
return super.onKeyDown(keyCode, event);
}
it is not possible to intercept KEYCODE_HOME by external applications:
http://code.google.com/p/android/issues/detail?id=1979
I don't think you can intercept HOME key presses. It is reserved to ensure that you don't get locked within any application.
KEYCODE_HOME : This key is handled by the frame work and is never delivered to applications.
So you can't use it

Categories

Resources