I am using a repeating alarm rtc wakeup to call my service every hour. The service sends a notification. The problem that I am facing is that whenever I go to the home page and press the square button and close my app from the app stack I believe that a new instance of my service is being created. My app is a timetable so whenever this happens the notifications start from the first class that I have to attend instead of continuing from where it was. Can anyone tell me how to prevent a new instance from being created.
-
Looke here: http://stackoverflow.com/a/3692966/3332634 – yshahak Jul 26 '15 at 12:23
1 Answers
The problem that I am facing is that whenever I go to the home page and press the square button and close my app from the app stack I believe that a new instance of my service is being created
Your process is not guaranteed to survive between alarms.
My app is a timetable so whenever this happens the notifications start from the first class that I have to attend instead of continuing from where it was.
Your service needs to persist its information, in a database, SharedPreferences
, other form of file, or perhaps out on the Internet.
Can anyone tell me how to prevent a new instance from being created.
You can't. The user can get rid of your service whenever the user wants to, and Android can terminate your process to free up system RAM for other apps. Only have your service running when it is actively delivering value to the user. Use files or other persistent stores for holding data between runs of the service.

- 986,068
- 189
- 2,389
- 2,491
-
Thank you CommonsWare but there's one thing that I don't understand how is a new service starting when the app is closed,I know for certain that that a new service is starting because I am receiving the same notifications that I created again.Please mention if you want to have a look at my code. – Code-crazy9 Jul 26 '15 at 13:03
-
@Code-crazy9: "how is a new service starting when the app is closed" -- well, you said that you used `AlarmManager` to start your service every hour. If you do not want `AlarmManager` to start your service every hour, get rid of the code where use used `AlarmManager` to start your service every hour. – CommonsWare Jul 26 '15 at 13:05
-
:I thought that a service is supposed to terminate when the user closes the app from the app stack.Please correct me if I am wrong. Assuming that I am right my question is how is the service starting the moment I close my app and remove it from the app stack.I want my service to continue when the app is in the back ground – Code-crazy9 Jul 26 '15 at 13:16
-
@Code-crazy9: "I thought that a service is supposed to terminate when the user closes the app from the app stack" -- normally, the process terminates when the user swipes an app off the overview screen/recent tasks list. That will get rid of the service. "how is the service starting the moment I close my app and remove it from the app stack" -- perhaps you are returning `START_STICKY` or `START_REDELIVER_INTENT` from `onStartCommand()`. Or, perhaps you did not override `onStartCommand()`, in which case the default is `START_STICKY`. – CommonsWare Jul 26 '15 at 13:20
-
:yeah I didn't override onStartCommand() I will try it out thanks for the advice. – Code-crazy9 Jul 26 '15 at 13:25
-
@Code-crazy9: Go with `START_NOT_STICKY` if you do not want the service to restart. Or, better yet, call `stopSelf()` to get rid of the service when you no longer need it, rather than have it linger while the process is around. – CommonsWare Jul 26 '15 at 13:26