4

i have problem that beetwen two subsequent invokes of startService the private data of IntentService is cleared - i assume that the service is stopped after first invoke is finished, and then recreated when handling second invoke. Is there any way to force IntentService not to be recreated or to store this private data to be the same in two subsequent calls of startService routine?

2 Answers 2

3

If you want to try to keep data across instances, you can put it in a static variable. A typical example: you have some state you load of disk, so you make a global singleton that loads the data when first instantiated, and then further requests for it use the same instance (without re-loading the data). When a new instance of your service retrieves the singleton in its onCreate(), it doesn't need to be re-loaded if a previous service instance had already requested it.

When doing such a design, it is very important you understand the lifecycle of your process and when it can be killed. Otherwise you can lose data at fairly random points when your process is killed during normal operation.

2

i assume that the service is stopped after first invoke is finished, and then recreated when handling second invoke.

No. It will only stop when all Intents received have been processed.

2
  • 1
    Ok so lets assume that the first intent was sent, then it was procesed by IntentService and the IntentService was stopped. Some time after it, the second intent was sent - how can I restore the context of IntentService from handling the previous Intent?
    – miqqo
    Feb 5, 2011 at 22:50
  • 1
    @miqqo: You don't. That is the whole point. In between invocations, the IntentService is not in RAM. In fact, with any luck, nothing of your entire application will be in RAM. You need to handle this like any other application in any other operating system -- store stuff between runs in a persistent store, such as a database. Feb 5, 2011 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.