startForeground() causing problems with cordova

I'm using a service in my cordova app which generates a startForeground notification in the onCreate. If the app is killed via swipe and started again, the cordova device-ready event isn't fired anymore. It seems the cordova object itsself is not defined when the app is restarted. I get the following error message:

E/Web Console(19472): Uncaught TypeError: Object #<Object> has no method 'exec' at file:///android_asset/www/src/jsFile.js:31

Line 31 and following:

cordova.exec(function (r) {
            if (callback) {
                callback(r);
            }
        }, function (err) {
            if (callback) {
                callback(false);
            }
        }, "Plugin", "functionName", []); 

The problem only occurs if I use the startForeground in my service, if I comment this part of the code out it's all working properly. I desperately need some help on these questions:

  • Is it possible to get cordova running manually?
  • How to start the service with startForeground, but still trigger the cordova device-ready after killing and restarting the app

Edit:

After making two quick example projects, one in Cordova 3.6 (the version I'm using for my actual App) and on with Cordova 5.1, I realized that the problem does not occur in the new Cordova version. However I don't really want to update, because:

  • Never touch a running system
  • I don't know what might not work anymore in the never version, as I have installed many plugins in my App.
  • I'm lazy.

Any idea what could have changed from 3.6 to 5.1 that solved the bug, and whether I could update my 3.6 code accordingly?

Edit2:

This is not a duplicate of this question. The deviceReady callback was passed wrongly in this question. This is not the case in my app, and it wouldn't make sense to only work when I don't use startForeground in my service.


Solution 1:

It seems your having an issue surrounding your apps life cycle. When you swipe of or forcefully close the app, the apps process is killed so when restarting if you had not saved dependent objects or variables in the state bundle they will come back as null because the activity is assuming those things are available. Can you paste the code you have in the onCreate() am guessing thats where your defining cordova.