How can i disable android device back button click in Phonegap OpenUI5 application? Tried this but didn't work:
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady()
{
document.addEventListener("backbutton", function (e) {
e.preventDefault();
}, false );
}
Generally speaking, browsers don't allow you to disable the back button. This is for security/usability reasons so that you can't trap the user on your webpage. Hence, you need to think twice before doing it to make sure that you're actually trying to help your user.
That having been said, there are techniques described here that might work for you.
I would personally suggest that you do the right thing and rather than disable the back button you simply handle the event and do the appropriate clean up in your app.
In SAPUI5 you can use jQuery to listen to navigation events:
// event fired when the user changes the URL, e.g. forward or back navigation
// you need to implement the handleNavigate handler
$(window).on("navigate", handleNavigate)
// event fired when the user tries to close the browser or reload the page
// you need to implement the handleBeforeUnload handler
$(window).on('beforeunload', handleBeforeUnload);
Related
I'm looking for change action for backbutton, actually I'm using events to catch backbutton calls and using navigator.app.exitApp(); to close app (in really pause app), but I need backbutton call action like home button.
Why? Well, I using cordova-plugin-background-mode, and this works only when app send to background (if using navigator.app.appExit() app is paused), so I need leave app every in background.
Is possible change action of backbutton to works like home?
I want use in iPhone and Android systems
you can use the plugin
https://github.com/amitsinha559/cordova-plugin-back-as-home
and
https://github.com/tomloprod/cordova-plugin-appminimize
after adding the plugin you can minimize the app when ever you want
like on back button click
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
window.plugins.appMinimize.minimize();
}
Whichever suits you. Unfortunately they both are for android. you need to create plugin manually for iPhone.
I am creating an application using the framework cordova. I need to intercept the "Back" button of the phone itself in order to perform the various aoperazioni, for example closing a modal dialog. The problem is that now, once you open the modal, I can not close it by pressing the back button. What happens is that you close any application. How could I do to intercept this event which is not part of the app but the phone? I refer to the actual button of a smartphone. Thanks for your help.
You can achieve this with Javascript using Phonegap's Event API
document.addEventListener("backbutton", function (e) {
// your code
} , false);
Have you read the docs?
Here's what you need: http://docs.phonegap.com/en/4.0.0/cordova_events_events.md.html#backbutton
document.addEventListener ('backbutton', function (evt) {
if (x) return // Proceed normally.
// Do stuff.
})
Adding the back button event listener overrides all the default behavior. (HTML5 History pop listener)
But, that's not what I want. I want the back button to potentially not fire (i.e.: something like evt.preventDefault), allowing the HTML5 History pop event to run. HELP!
it will override the default one. as in the documentation there is no need to prevent the default behavior.
But since in Cordova, "Default behavior" Is just exiting the app you can make it do the same by:
if (x) navigator.app.exitApp();
I'm developing a app using angularjs and ionic framework. It has a logout option also. When i logout from my app it goes straight to the home page.But if i press the back button it'll go to page that can be accessed only if you are logged in. How can i create a option to close the app if you press the back button from the home page? I use angularjs.
I used this way but did not work
document.addEventListener("backbutton", function(e){
if($.mobile.activePage.is('/templates/playlists')){
/*
Event preventDefault/stopPropagation not required as adding backbutton
listener itself override the default behaviour. Refer below PhoneGap link.
*/
//e.preventDefault();
navigator.app.exitApp();
}
else {
navigator.app.backHistory()
}
}, false);
Your code is good.
But, I'm not sure about the second line : are you sure that the active page id is /templates/playlists ?
Normally, the value you should pass to the $.mobile.activePage.is function should contain a page id (which could be retrieved like this : $.mobile.activePage.attr('id')).
(by the way, I don't know which of jQuery Mobile you are using, but the $.mobile.activePage.is was deprecated).
Is it possible to capture a press and hold event of an Android Devices back button to close the app?
Cheers
Paul
yes, see the PhoneGap API:
http://docs.phonegap.com/en/1.0.0/phonegap_events_events.md.html
// Call onDeviceReady when PhoneGap is loaded.
//
// At this point, the document has loaded but phonegap.js has not.
// When PhoneGap is loaded and talking with the native device,
// it will call the event `deviceready`.
//
document.addEventListener("deviceready", onDeviceReady, false);
// PhoneGap is loaded and it is now safe to make calls PhoneGap methods
//
function onDeviceReady() {
// Register the event listener
document.addEventListener("backbutton", onBackKeyDown, false);
}
// Handle the back button
//
function onBackKeyDown() {
}
No there isn't a listener only for that but I think you can make your own long back key listener by using onKeyDown() listener or even better, you could give a try with onLongKeyDown() listener...
Good luck!