1

I have an application where I extend Application class and override the onCreate method.
In my onCreate, I schedule a service to run every 5 minutes using AlarmManager.
In the logs, I see that onCreate get's called again after a while as though the application was restarted / recreated.
this ends up with my service not being synchronized with the first execution.
Is there a way to prevent this and to guaranty code in Application's onCreate will execute exactly once (assuming of course the application wasn't explicitly closed by the user)?

Thanks

Oren
  • 937
  • 1
  • 10
  • 34
  • Can you be a bit more specific? Is this happening if you change the orientation of the device? – kittu88 Oct 15 '12 at 12:42
  • I guess orientation changes several times, but this is when application is in background and also, this should affect activities, not application. in the logs I see it a few times on a period of more than 20 minutes so I guess it's not related to configuration... – Oren Oct 15 '12 at 12:48

1 Answers1

1

onCreate() on your Application is called as part of starting up your process. If you are seeing it called several times in the logs, that is because your process was terminated in between runs.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • So is the AlarmManager restarting it? How can I prevent / detect such cases in order to maintain correct service execution in defined interval? – Oren Oct 15 '12 at 12:51
  • @Oren: "So is the AlarmManager restarting it?" -- since I do not have access to your logs, I have no way of answering that. You should be able to tell that based on log timestamps. "How can I prevent / detect such cases in order to maintain correct service execution in defined interval?" -- don't start up a service from `onCreate()` of `Application`. Or, keep track of when your service runs (e.g., `lastRunTime` value in `SharedPreferences`) and only kick off `AlarmManager` if you determine that your service has not been running. – CommonsWare Oct 15 '12 at 12:56
  • from the logs I see that the process is recreated (different pid) when I enter the main activity after some time - meaning the process is recreated in some cases to open the activity. I don't want to use SharedPreferences because this will mean I will have to maintain many use cases (e.g. boot, closing from task manager etc) – Oren Oct 15 '12 at 14:54
  • @Oren: You cannot stop `onCreate()` from being called. You cannot stop your process from being terminated. – CommonsWare Oct 15 '12 at 14:55
  • That I understand. what I'm asking is if there is a way that calls to AlarmManager to schedule a service will not be called multiple times because of android's processes management... – Oren Oct 15 '12 at 15:00
  • @Oren: If you use an equivalent `PendingIntent` each time, each call to `setRepeating()` should replace the old one. Or, you can manually `cancel()` the old one, if it exists. – CommonsWare Oct 15 '12 at 15:02
  • Yes, what I needed was that the service will be executed by AlarmManager at fixed interval, and I can't achieve it this way as Process gets teminated and recreated and calls to AlarmManager are re-executed – Oren Oct 16 '12 at 13:48
  • @Oren: You need to keep track of when your service runs, anyway, as there are various scenarios when your alarms get unscheduled (reboot, force stop). so, as I suggested, keep a `lastRunTime` somewhere, and if it has been significantly over your polling period since the last time your service run, reschedule the alarms. – CommonsWare Oct 16 '12 at 14:03
  • Strictly speaking this is not entirely true. Using different processes, as well as some crash-reporting frameworks can result in `Application.onCreate()` being called multiple times. See [related](http://stackoverflow.com/questions/12809737/android-application-class-method-oncreate-being-called-multiple-times) [posts](http://stackoverflow.com/a/3951911/383414). – Richard Le Mesurier Aug 22 '16 at 11:00