Cordova Android: handling back button - android

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

Related

PhoneGap on click backbutton simulate click in home button

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.

Revert to default behavior for physical back button

I have a Cordova app made with ReactJS. When on the homepage I would like the back button to suspend the app (default behavior) and when not on the home page I would like to have my custom implementation of history to take over. In the deviceready event handler I added
document.addEventListener( 'backbutton', onBackButtonKeyDown.bind(this), false);
The implementation of onBackButtonKeyDown looks like this (removed some stuff to make the code clearer):
function onBackButtonKeyDown(e) {
if(!SomeStore.isMainMenuClosed) {
e.preventDefault();
historyButtonActions.raiseBackButtonClickedEvent();
}
};
I also tried adding an else block with return false;. In all cases the default behavior is not triggered. While Googling I saw some suggestions to use navigator.app.exitApp();, but I don't want to exit the app. I want the Home intent to take over and for the app to suspend like the default functionality works.
I ended up side stepping the issue by adding and removing the custom handler from the backbutton event as needed. Once the handler is removed, the default behaviour returns.

Android hardware back button action never fires (Ionic)

I tried adding function to override current back button behaviour, which is exactly the same as pressing back button (left arrow) in browser. This often goes to the view that is forward instead of going back. I understand that this is how WebView would handle back button action if it is not override.
I was surprised that Android back button doesn't work the same as ion-nav-view standard back button, so I found the code that is responsible in ionic.bundle.js:
// Triggered when devices with a hardware back button (Android) is clicked by the user
// This is a Cordova/Phonegap platform specifc method
function onHardwareBackButton(e) {
console.log('check'); //this never fires
var backView = $ionicHistory.backView();
if (backView) {
// there is a back view, go to it
backView.go();
} else {
// there is no back view, so close the app instead
ionic.Platform.exitApp();
}
e.preventDefault();
return false;
}
console.log('check2'); //this fires
$ionicPlatform.registerBackButtonAction(
onHardwareBackButton,
IONIC_BACK_PRIORITY.view
);
I tested it on Galaxy S5.
Any hints how to make this work?
Why are you not using : $ionicHistory.goBack() instead of backView.go(); ?
From doc goBack is the way to navigate through history
I think you are adding the the back view as a new view upon the stack by calling
backView.go();
you should, instead call
$ionicHistory.goBack();
Is it possible you're using Ionic View to run the app? If so, that's been one of the many different-than-native issues that I've run into while using Ionic View. I couldn't get the back button behavior to work as intended within Ionic View, among other things.
I've since switched to testing my app with others using Google Play Alpha/Beta Testing. That will get the app running on every tester's device the way it will actually appear.
Even though I had no errors like:
cordova is undefined
I added the following to my index.html and it started working
<script src="cordova.js"></script>

Close the application on back button of android phones

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).

Reset history in html when on home page?

Okay, I'm making app with phonegap, and in the app I have a home button which goes to the home screen of the app, but if I click the back key after I touched the home button it goes to the page I was on before I clicked the home button, is it possible to reset the history when you navigate to the homepage? Thanks :)
You can't clear the history.
You can listen for back button event:
document.addEventListener("backbutton", onBackKeyDown, false);
function onBackKeyDown() {
// Handle the back button
}
and stop the propagation.
The link below recommends using location.replace(url) instead of replacing the history completely, which I'm not sure is possible anyway. However, given that this is a constrained environment (an app) wiping the history might make sense - again, if possible if you're in an app.
Clear browser history
Alternatively, couldn't you just remove the back key and replace it with a custom one?
inside the event handler of the button which takes you to home:
startActivity(activityForHome); //I guess you already have this line
finish(); //now add this line

Categories

Resources