46

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.

Hardik4560
  • 3,202
  • 1
  • 20
  • 31
Michael Kunst
  • 2,978
  • 25
  • 40
  • 1
    can you edit your question and provide the JAVA code (the plugin code). – Karan Kumar Aug 17 '15 at 10:32
  • 1
    It doesn't matter (99.9% sure), as I can call any Plugin I want and the error stays the same. – Michael Kunst Aug 19 '15 at 07:14
  • Just a simple suggestion : if you still have the problem (I guess you found a solution), is your `useCapture` parameter set to `false` at the end of your event listening : `addEventListener('deviceready', onDeviceReady, false);` ? – Ivan Gabriele Jun 15 '16 at 01:53
  • Hi Ivan. Yes it is, was one of the first things I tried :) Problem has been solved by updating cordova (which was a pain though). Thanks none the less. – Michael Kunst Jun 15 '16 at 07:13
  • Only a idea but you say you start a service. Which means a decoupled thread from your application main thread running that service. I think you have to stop and remove your service when the app is closing. Otherwise, parts of your app are still running or android thinks its still running and when you reopen the app it will never call ready because it thinks that already happend, depending on the dead service you produced. – Rene M. Dec 01 '16 at 17:26
  • 1
    Possible duplicate of [Uncaught TypeError: Object # has no method 'exec' at file:///android\_asset/www/index.html](https://stackoverflow.com/questions/13554251/uncaught-typeerror-object-object-has-no-method-exec-at-file-android-ass) – Martin Zeitler Jun 09 '17 at 17:26
  • @MartinZeitler, I added an explanation why this is not a duplicate of the question you linked. – Michael Kunst Jun 12 '17 at 12:05

1 Answers1

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.

Smile
  • 207
  • 4
  • 13