Android : Keeping reference of an object for a task's lifetime

on Monday, August 11, 2014


I am looking for a way to keep reference of an object/instance that's a member object of a service. I know in case of Activities, we can save it by the provided onSaveInstance method. However, I couldn't find any similar way for a service.


I can use static object but it's unsafe as there is no guaranty, if the reference would be valid the next time, the service is created.


Another possible way would be, I store all the data on storage & later on when the service is created again, I can read the data from storage. Then traverse through information/data provided by the another class & find the relevant reference for the info read from the storage, which I think is very expensive interms of execution time.


So I am looking for a way to get rid of all this process & keep a strong reference (of the object in service) in the memory as long as the task/app is running.


Regards


0 comments:

Post a Comment