Android hardware back button action never fires (Ionic) - android

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>

Related

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.

Onsen ui:Disable device backbutton in android didnt work

I am developing an android application using Cordova and Onsenui, in whic i want to disable the android back button handler.
I've tried this
answer ,but didnt work for me
Here is my code its placed on top of my app.js file
ons.ready(function() {
ons.disableDeviceBackButtonHandler();
});
Supposing that you are using <ons-navigator var="myNavigator"></ons-navigator>, have you already tried something like this?
// To disable a navigator back button handler
myNavigator.getDeviceBackButtonHandler().disable();
You should also not forget to add cordova.js reference to your index page. You don't need to paste the file, it will be generated automatically every time you deploy a Cordova app.
<script src="cordova.js"></script>
If you don't add it, it will not be possible to manipulate the BackButton behavior.
Default behavior:
Main page: closes the app.
Other pages: goes back.
Disabling the handler, the app will be closed every time you press the button, regardless the page.

Prevent closing InAppBrowser - Cordova

I have an simple app that onDeviceReady starts the InAppBrowser and shows a website. Then The InappBrowser will be closed when a specific event occurs.
As you may know, on Android platform pressing "back button" closes the InAppBrowser which I want to be prevented. I want to InAppBrowser be shown to user until that event occurs and user be unable to close the InAppBrowser.
Be noted that I am not talking about hardwareback options. hardwareback is a useful option which lets user goes back in history of his/her navigation by pressing "back button" but at the first page (when there is nothing left in history), it closes InAppBrowser while I want InAppBrowser still remains open.
In the latest InAppBrowser versions you still have to change the onBackPressed function in the JAVA code of InAppBrowserDialog.java plugin code:
public void onBackPressed () {
if (this.inAppBrowser == null) {
this.dismiss();
} else {
// better to go through the in inAppBrowser
// because it does a clean up
if (this.inAppBrowser.hardwareBack() && this.inAppBrowser.canGoBack()) {
this.inAppBrowser.goBack();
} else {
// this.inAppBrowser.closeDialog();
}
}
}
I don't like this approach I have asked a feature on GitHub; you can follow it on GitHub here: https://github.com/apache/cordova-plugin-inappbrowser/issues/530
I realize this question is fairly old but I stumbled upon it when I was searching for the answer myself.
You can edit the InAppBrowserDialog.java file (plugins>cordova-plugin-inappbrowser>src>android) to change the behavior of the back button once it's pressed.
this.inAppBrowser.closeDialog()
Comment out the above piece of code in the onBackPressed() method, and this will prevent the browser from being closed on hardware back, while still maintaining the navigation functionality.
Actually, there is no solution available to prevent the inappbrowser from closing when the hardware back button is pressed. The only thing you can do is reload the same URL in the inappbrowser again after a minimum delay of 500ms. The delay is important, otherwise, the inappbrowser window won't be shown.

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

PhoneGap/Cordova blank screen on pause/resume

I have a simple html/javascript app that starts and works up fine on an Android device. However, when the app is put in background by using the 'home button' and then brought back to the foreground by clicking on the app again - it starts with a blank screen (white blank screen on android ) and stays that way until the 'back button' is pressed.
Once the 'back button' is pressed the screen refreshes to the last page displayed by the app.
I am new to PhoneGap and I am sure there is something simple/fundamental that I am missing - in terms of how to handle the 'resume' event/etc.. I have followed the instructions provided on this link by Phil Mitchell http://wiki.phonegap.com/w/page/35501397/Tutorials (which is a great resource btw..)
Thanks.
Update :
After looking at the DroidGap code, I have tried to add the following line :-
super.setBooleanProperty("keepRunning", false);
But this does not seem to help. I am happy for the app to exit every time the home button is entered and then do a full restart when the app is clicked on the mobile.
Any help is much appreciated..
I was having the same problem and I want to share a hack that worked for me.
My app is based on ionic/angular/cordova and the android version was giving me a white screen around 50% the times on pause/resume.
When it happened, tapping anywhere on the screen or hitting the back button would make the screen to render again just fine.
I added a div tied to a scope variable named random, like this:
<body ng-app="starter" id="body">
<div>{{ random }}</div>
<ion-nav-view>
</ion-nav-view>
</body>
Then I added a listener inside my app.js to capture the resume event fired by cordova, to then change the random variable and call $apply(), like this:
document.addEventListener("resume", function() {
angular.element(document.querySelector('#body')).scope().random = Math.random();
angular.element(document.querySelector('#body')).scope().$apply();
})
Since the ion-nav-view takes over the whole screen that div never shows up, but angular doesn't know that and refreshes the page, causing the whole thing to work.
(I tried to tie the variable to an invisible element but then angular doesn't refresh the page)
I hope this helps someone!
There seem to be issues around rendering the pages thru PhoneGap and 'Dialog'ing thru Client side FB authentication flow using the JS SDK. If I disable the client side authentication, then the rendering and display of the page works very well, when the app is brought to foreground from background (pause/resume/etc).
I am assuming that the page will need to sense that it is being displayed over a mobile device and use the IOS or Androi SDK for client side authentication.. (if/when I get around to testing that, I update the answer - if there is anybody who has any more light to shed, please feel free to embellish).
If you still say "I am happy for the app to exit every time the home button is entered and then do a full restart when the app is clicked on the mobile." then go for it.
#Override
protected void onStop(){
super.onStop();
}
Try this from where you create activity or from the class which "extends DroidGap".

Categories

Resources