[Android] $ionicView.beforeEnter not firing


#1

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

#2

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.


#3

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.


#4

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