Onsen 'init' event not fired, or fired randomly on Android 4.1



  • I am trying to figure out why the ‘init’ event is not firing in Android 4.1, I have tried placing the code in all places, in $(document).ready, in onDeviceReady of Cordova, but still not working as expected.
    Here is how I am setting up the event listener:

    In the html:

    <body onload="app.initialize()">
    

    in the javascript:

    var app = {
        initialize: function() {
            this.bindEvents();
        },
        
        bindEvents: function() {
            document.addEventListener('deviceready', this.onDeviceReady, false);
        document.addEventListener('init', this.onInit, false);
        },
        
        onDeviceReady: function() {
            app.receivedEvent('deviceready');
        },
        
        onInit: function () {
            app.receivedEvent('init');  
        },
    
        receivedEvent: function(id) {
            if (id == "init") {
                alert("init ok");
            } else if (id == "deviceready") {
                alert("deviceready ok");
            }
        }
    };
    

    I tried everything and can’t figure out why it fires or not so randomly, one time first it fires the init event, then the deviceready event, other time the other way around, and other time only the deviceready event.

    Anyone else had such problems?


  • Onsen UI

    @Borislav Why are you adding the event listeners on load? Just add them at the beginning of the app without waiting for anything else.



  • @Fran-Diox What do you mean by beggining of the app?



  • @Borislav Meaning, just place them at the start of your script block. No need to wrap them in an initializing block.



  • This still does not answer the main problem - why is this occuring only in Android 4.1, and not above that?



  • @Borislav Can you run this codepen and report what happens?



  • @munsterlander said:

    @Borislav Can you run this codepen and report what happens?

    this way it seems to work, but since I am making an app with Cordova, first I need to know if ‘deviceready’ has fired, and then wait for other events, like ‘init’. If I do it the way you suggested and put all listeners in the begining of the script, ‘init’ event always fires before ‘deviceready’ event.

    EDIT: Actually no, just tested this way of setting listener for ‘event’ in my production app, and the ‘init’ event does not get fired at all, but other onsen events like ‘show’ are just fine



  • @Borislav So did it work or did it not fire, apologies, confused by your edit? Also, what are you wanting to initialize? Personally, I use the show event as it works better for my purposes (usually doing data binding updates).



  • The problem was solved when using the Crosswalk plugin, it makes Onsen UI works as expected on Android 4.4 and lower


Log in to reply