I have an activity that starts a service. There is also a singleton Data involved. When I close the activity by going to the homescreen, the Data singleton gets destroyed after a while, but, as it should, the service keeps running. Therefore some fields of Data become null again, and the service encounters nullpointers.
How can I prevent this from happening?
As far as I know,
Activityinstances get launched inside their own classloader, for security. Any singleton attached to a class that gets loaded as a result of loading yourActivityis tied to that classloader, which is in turn tied to the activity lifecycle.That is, I don’t think you can guarantee that this won’t happen, and therefore, shouldn’t rely on it.
You need to serialize any persistent state and then unserialize it when the
Activitystarts.