[Android] $ionicView.beforeEnter not firing

The goal

To have an event fire, preferably before the view is loaded, so I can reach out to the server and gather information for that view.

The problem

The $ionicView events are not firing, or at least not being detected by AngularJS. If I run the application in (ionic serve) the browser, there are no problems, and all events fire as they should.

What I’ve tried

What I usually do (for a web application):

angular.element(document).ready(function () {
    // ...
});

A more globalized attempt:

$rootScope.$on("$routeChangeStart", function(event, next, current) {
    console.log(event, next, current);
});

A controller scoped attempt:

$scope.$parent.$on('$ionicView.beforeEnter', function(ev, info) {
    console.info('$scope.$parent beforeEnter=' + info.stateName);
});
$scope.$on('$ionicView.loaded', function(ev, info) {
    console.info('node loaded');
});
$scope.$on('$ionicParentView.beforeEnter', function(ev, info) {
    console.info('parentview node before enter');
});
$scope.$on('$ionicView.beforeEnter', function(ev, info) {
    console.info('node before enter');
});
$scope.$on('$ionicParentView.enter', function(ev, info) {
    console.info('parentview node enter');
});
$scope.$on('$ionicView.enter', function(ev, info) {
    console.info('node enter enter');
});

Here is my ionic info:

cli packages: (/usr/local/lib/node_modules)

@ionic/cli-utils  : 1.15.2
ionic (Ionic CLI) : 3.15.2

global packages:

cordova (Cordova CLI) : 7.1.0 
Gulp CLI              : CLI version 3.9.1 Local version 3.9.1

local packages:

Cordova Platforms : android 6.3.0 ios 4.5.2
Ionic Framework   : ionic1 1.3.3

System:

Android SDK Tools : 26.1.1
Node              : v8.6.0
npm               : 5.3.0 
OS                : Linux 4.13

Environment Variables:

ANDROID_HOME : /home/dave/Android/Sdk

Misc:

backend : pro

For what its worth, I had the same problem after updating to ionic 1.3.x for a v1 project. Downgrading to 1.2.4 made the lifecycle hooks work again. There is a GitHub issue on that where folks talked about how to solve this in 1.3.x but none of the tips worked for me. I had this issue on both iOS and android. Don’t know about desktop browser - never tried it.

I just downgraded ionic to v1.2.4 from ionic@latest…

cd app-directory
sudo npm uninstall -g ionic
sudo npm install -g ionic@1.2.4
ionic serve

This fixed all of my problems, even some issues I haven’t even started working on a fix for. Yikes.

EDIT

Hang on, shutting down ionic serve and launching ionic emulate android -lc had fantastic results until I made a change in the controller JavaScript and reloaded the page. Reverting the change did not fix the problem, and I’m now back to the same issue as I originally posted.

It seems that in conjunction with the downgrade, using the following in my controller seems to have the most consistent results:

function doLeave() {
    // ...
}
function doStart() {
    // ...
}
$scope.$on('$locationChangeStart', function locationChangeStart() {
    doLeave();
});

$scope.$on('$ionicView.beforeLeave', function beforeLeave(){
    doLeave();
});

$scope.$on('$ionicView.beforeEnter', function beforeEnter(){
    doStart();
});

I know this is quite old, however i wanted to add that on Ionic v1.3.4 (the last version) the $ionicView.beforeEnter && $ionicView.afterEnter all work. But I don’t ever recall having issues on any version of 1.3.x.

That being said, I came to this thread because I am updating an older ionic v1 app and ran into a problem where the $ionicView.beforeEnter was not firing and I could not figure out why. It turns out the template that goes with that controller was missing closing HTML tags on a few <i class="icon> elements…I didn’t add the closing </i>

Thus, apparently, an element with a missing closing tag was preventing the controller $ionicView.beforeEnter/afterEnter functions from firing. I had NEVER seen that before so it took me a while to figure out the issue was in my template and not in my controller.

For what its worth, hope my discovery helps those in the future.