Skip to content

Commit c3544d8

Browse files
author
Adam Bradley
committed
Android back button fixes, closes #454
1 parent b20ce80 commit c3544d8

File tree

5 files changed

+46
-13
lines changed

5 files changed

+46
-13
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
11

2+
## 0.9.23-alpha (pre-release)
3+
- Android back button correctly goes back a view or closes the app
4+
- CustomEvent polyfill improvements for Android
5+
6+
27
## 0.9.22 "Alpha Narwhal" (2014-01-30)
38
- Tap polyfill overhaul to remove 300ms delay when firing a click
49
- Android click firing twice fixes

dist/js/ionic-angular.js

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,8 +469,7 @@ angular.module('ionic.service.modal', ['ionic.service.templateLoad', 'ionic.serv
469469
};
470470
}]);
471471
;
472-
(function() {
473-
'use strict';
472+
(function(ionic) {'use strict';
474473

475474
angular.module('ionic.service.platform', [])
476475

@@ -495,7 +494,7 @@ angular.module('ionic.service.platform', [])
495494
* @param {function} cb the callback to trigger when this event occurs
496495
*/
497496
onHardwareBackButton: function(cb) {
498-
this.ready(function() {
497+
ionic.Platform.ready(function() {
499498
document.addEventListener('backbutton', cb, false);
500499
});
501500
},
@@ -506,7 +505,7 @@ angular.module('ionic.service.platform', [])
506505
* @param {function} fn the listener function that was originally bound.
507506
*/
508507
offHardwareBackButton: function(fn) {
509-
this.ready(function() {
508+
ionic.Platform.ready(function() {
510509
document.removeEventListener('backbutton', fn);
511510
});
512511
},
@@ -625,8 +624,8 @@ angular.module('ionic.service.templateLoad', [])
625624
angular.module('ionic.service.view', ['ui.router'])
626625

627626

628-
.run( ['$rootScope', '$state', '$location', '$document', '$animate',
629-
function( $rootScope, $state, $location, $document, $animate) {
627+
.run( ['$rootScope', '$state', '$location', '$document', '$animate', '$ionicPlatform',
628+
function( $rootScope, $state, $location, $document, $animate, $ionicPlatform) {
630629

631630
// init the variables that keep track of the view history
632631
$rootScope.$viewHistory = {
@@ -673,6 +672,21 @@ angular.module('ionic.service.view', ['ui.router'])
673672
}
674673
});
675674

675+
// Triggered when devices with a hardware back button (Android) is clicked by the user
676+
// This is a Cordova/Phonegap platform specifc method
677+
function onHardwareBackButton(e) {
678+
if($rootScope.$viewHistory.backView) {
679+
// there is a back view, go to it
680+
$rootScope.$viewHistory.backView.go();
681+
} else {
682+
// there is no back view, so close the app instead
683+
navigator.app.exitApp();
684+
}
685+
e.preventDefault();
686+
return false;
687+
}
688+
$ionicPlatform.onHardwareBackButton(onHardwareBackButton);
689+
676690
}])
677691

678692
.factory('$ionicViewService', ['$rootScope', '$state', '$location', '$window', '$injector',

dist/js/ionic-angular.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

js/ext/angular/src/service/ionicPlatform.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
(function() {
2-
'use strict';
1+
(function(ionic) {'use strict';
32

43
angular.module('ionic.service.platform', [])
54

@@ -24,7 +23,7 @@ angular.module('ionic.service.platform', [])
2423
* @param {function} cb the callback to trigger when this event occurs
2524
*/
2625
onHardwareBackButton: function(cb) {
27-
this.ready(function() {
26+
ionic.Platform.ready(function() {
2827
document.addEventListener('backbutton', cb, false);
2928
});
3029
},
@@ -35,7 +34,7 @@ angular.module('ionic.service.platform', [])
3534
* @param {function} fn the listener function that was originally bound.
3635
*/
3736
offHardwareBackButton: function(fn) {
38-
this.ready(function() {
37+
ionic.Platform.ready(function() {
3938
document.removeEventListener('backbutton', fn);
4039
});
4140
},

js/ext/angular/src/service/ionicView.js

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
angular.module('ionic.service.view', ['ui.router'])
22

33

4-
.run( ['$rootScope', '$state', '$location', '$document', '$animate',
5-
function( $rootScope, $state, $location, $document, $animate) {
4+
.run( ['$rootScope', '$state', '$location', '$document', '$animate', '$ionicPlatform',
5+
function( $rootScope, $state, $location, $document, $animate, $ionicPlatform) {
66

77
// init the variables that keep track of the view history
88
$rootScope.$viewHistory = {
@@ -49,6 +49,21 @@ angular.module('ionic.service.view', ['ui.router'])
4949
}
5050
});
5151

52+
// Triggered when devices with a hardware back button (Android) is clicked by the user
53+
// This is a Cordova/Phonegap platform specifc method
54+
function onHardwareBackButton(e) {
55+
if($rootScope.$viewHistory.backView) {
56+
// there is a back view, go to it
57+
$rootScope.$viewHistory.backView.go();
58+
} else {
59+
// there is no back view, so close the app instead
60+
navigator.app.exitApp();
61+
}
62+
e.preventDefault();
63+
return false;
64+
}
65+
$ionicPlatform.onHardwareBackButton(onHardwareBackButton);
66+
5267
}])
5368

5469
.factory('$ionicViewService', ['$rootScope', '$state', '$location', '$window', '$injector',

0 commit comments

Comments
 (0)