开发者

Is there any way to force the IntentService not to be recreated?

开发者 https://www.devze.com 2023-02-09 00:19 出处:网络
i have problem that beetwen two subsequent invokes of startService the private data of IntentService is clea开发者_开发知识库red - i assume that the service is stopped after first invoke is finished,

i have problem that beetwen two subsequent invokes of startService the private data of IntentService is clea开发者_开发知识库red - 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消