Disable physical back button in Android?

7,303 views
Skip to first unread message

Adam Biggs

unread,
Apr 12, 2011, 2:15:21 PM4/12/11
to phon...@googlegroups.com
Is there a way to completely disable this button?

Simon MacDonald

unread,
Apr 12, 2011, 2:24:35 PM4/12/11
to phon...@googlegroups.com
Register an event listener in the onLoad method of your page:

document.addEventListener("backbutton", backKeyDown, true);

Once you get the deviceready event override the back button behaviour:

navigator.app.overrideBackbutton(true);

Then provide and empty function for backKeyDown:

function backKeyDown() {}

I'm sure you'll be able to simplify that but you see what I mean.

Simon Mac Donald
http://hi.im/simonmacdonald

On Tue, Apr 12, 2011 at 2:15 PM, Adam Biggs <adam.v...@gmail.com> wrote:
> Is there a way to completely disable this button?
>

> --
> You received this message because you are subscribed to the Google
> Groups "phonegap" group.
> To post to this group, send email to phon...@googlegroups.com
> To unsubscribe from this group, send email to
> phonegap+u...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/phonegap?hl=en?hl=en
>
> For more info on PhoneGap or to download the code go to www.phonegap.com
>

Adam Biggs

unread,
Apr 12, 2011, 8:04:46 PM4/12/11
to phon...@googlegroups.com
Thanks a lot for your reply, I'll try that.

Another quick question - is there a way to programatically close the app? If possible I'd like to make the back button do the same as the home button (instead of doing absolutely nothing). If not I understand.

Cheers

Martin Bay

unread,
Apr 13, 2011, 8:27:23 AM4/13/11
to phonegap
I use BackButton.exitApp(); to close the app.

Simon MacDonald

unread,
Apr 13, 2011, 10:30:11 AM4/13/11
to phon...@googlegroups.com
I would use:

navigator.app.exitApp();

which was introduced in 0.9.4 and it'll be the api going forward.

BBerisha

unread,
Apr 13, 2011, 2:02:17 AM4/13/11
to phonegap
Simply way, do it directly on java, extends Droidgap :

public class App extends DroidGap {
...
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) { // go back home
super.loadUrl("file:///android_asset/www/index.html");
return true;
}
if (keyCode == KeyEvent.KEYCODE_MENU) { //Exit app
this.finish();
return true;
}
return super.onKeyDown(keyCode, event);
}
}

dont forget to import android.view.KeyEvent

Robert Fletcher

unread,
Apr 13, 2011, 4:56:30 PM4/13/11
to phon...@googlegroups.com

Sorry bberish I think Simon's way is easier.

Adam Biggs

unread,
Apr 13, 2011, 9:35:42 PM4/13/11
to phon...@googlegroups.com
I tried what you suggested but I can't seem to get it to work. My code stalls at the "navigator.app.overrideBackbutton(true);" line and doesn't proceed (no errors in the console though). I'm using 0.9.4.

Simon MacDonald

unread,
Apr 14, 2011, 10:59:37 AM4/14/11
to phon...@googlegroups.com
My mistake, it doesn't look like that code is in 0.9.4.  It will be in 0.9.5.  In the meantime use:

BackButton.override();

Simon Mac Donald
http://hi.im/simonmacdonald


On Wed, Apr 13, 2011 at 9:35 PM, Adam Biggs <adam.v...@gmail.com> wrote:
I tried what you suggested but I can't seem to get it to work. My code stalls at the "navigator.app.overrideBackbutton(true);" line and doesn't proceed (no errors in the console though). I'm using 0.9.4.

--
Reply all
Reply to author
Forward
0 new messages