7

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.

Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
  • 3
    Remove the finish call, or I don't understand your question... – Stephane Mathis Jul 26 '13 at 07:23
  • The Back button can not be disabled on unrooted devices, security issues .. – g00dy Jul 26 '13 at 07:25
  • Check this post out: http://stackoverflow.com/questions/15834629/android-droidgap-disabling-back-button?rq=1 – Tinus81 Jul 26 '13 at 07:26
  • 1
    @Tinus81 yes, i followed that link and implemented as `appView`.. Can u please check the code i have placed?? – Avadhani Y Jul 26 '13 at 07:29
  • did u tried removing finish() and return false? – R9J Jul 26 '13 at 07:29
  • @g00dy i am using the device which is unrooted... still it is not working... – Avadhani Y Jul 26 '13 at 07:29
  • @R9j still i am able to navigate to the previous pages....not helpful... – Avadhani Y Jul 26 '13 at 07:31
  • @AvadhaniY - You want the Back button to be "non-functionning" - right? That's not possible by default. What you can do is to control the behaviour - if the back is pressed, the same aActivity gets loaded again, with the same views etc. You can not disable this button! However, the keypresses are controlled (called) only from an Activity, so you can start from there. – g00dy Jul 26 '13 at 07:34
  • @g00dy FYI, i am using `Phonegap`.... as `super.loadUrl("MY URL");`... The Device back button can be disabled in native code as `@Override public void onBackPressed(){ finish(); }`... In the same way how can i disable for a PhoneGap application... Can u please help in that way.... – Avadhani Y Jul 26 '13 at 07:37
  • @AvadhaniY The code you pasted exits the application - right? because it looks like the case to me ? – g00dy Jul 26 '13 at 07:41
  • Yes, The application exists with the above code i have placed.... but i want "do nothing" on click of Device back button... How can i acheive that – Avadhani Y Jul 26 '13 at 07:46
  • in the if condition again keep if(appView.canGoBack()){appView.goBack(); return whaturequired;} it and try... – Harish Jul 26 '13 at 12:38
  • @Harish i dont need to goBack... What i want is if Back is clicked nothing should happen.... – Avadhani Y Jul 26 '13 at 13:01
  • then why you are writtem finish() in onbackbuttonpress? – Harish Jul 26 '13 at 13:06
  • @Harish I just written for verifying that method is called or not onclick of Device back button... I am asking help to replace that line with the code that do nothing on click of device back button... can u help me in that way...??? – Avadhani Y Jul 26 '13 at 13:43

1 Answers1

23

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();
}
Avadhani Y
  • 7,566
  • 19
  • 63
  • 90
DaveAlden
  • 30,083
  • 11
  • 93
  • 155
  • 1
    Yeah i know how to do using `Phonegap`... but the problem is there are 100s of HTML pages in the server and i dont have a chance to handle/modify HTML content.. so, i am trying to get it done in Java level.. Cant i get through Java anyway...? – Avadhani Y Jul 26 '13 at 08:13
  • 2
    e.preventDefault(); is not working. I'm trying to remove the default event from android device back button and I tried the above code but no luck the application is still returning from previous page – GianFS Dec 30 '14 at 05:38
  • 1
    This code must be inside of "deviceready" event listener. If you are writing this directly into HTML, check also if you have 'unsafe-inline' in default-src to allow javascript inlining. – Honza Jan 01 '16 at 19:55
  • Instead of ```e.preventDefault```, you should use ```return false```. In my experience, calling ```e.preventDefault``` will cause ALL further events in Angular to be prevented. It's weird, but ```return false``` has the expected result. – sean.boyer Dec 06 '16 at 19:55