StuStirling
StuStirling

Reputation: 16211

Subclass of Application Ever Destroyed?

My Problem

I have a Service within which I monitor location changes. Its vital this stays running when in the background so it is a Foreground service.

My problem is whilst its in the background and things are getting destroyed, how do I save this data.

My Question

This brings me onto my potential solution.

I have a subclass of Application which I declare in my manifest file.

Would it be safe to see that any objects stored in this Singleton would be accessible throughout my Service's life and therefore give me a means to save my locations?

Thanks in advance

Upvotes: 1

Views: 146

Answers (3)

ian.shaun.thomas
ian.shaun.thomas

Reputation: 3488

I would say no. Even if you have a static class with static variables that you are setting, you have no guarantee that the OS will not arbitrarily kill your process at any time it chooses. Maybe 99% of the time what you are saying may work but it would not be a solid solution in my opinion.

You should, as suggested, store any important data in a database or in the android preferences, which ever is more logical for your application/data.

Upvotes: 1

devunwired
devunwired

Reputation: 63303

The short answer to your question is yes, Application basically lives as long as your process does so anything that you store there is as stable as it can be in memory until such time that Android may need to reclaim your app from the background to get more memory for a foreground application. However, not much benefit is added over simply saving your state data inside the Service itself. Android does not have a mechanism by which components out of an application are selectively destroyed to reclaim memory. If it needs additional memory to keep the foreground process happy, it simply kills the entire process of other apps in order of priority (based on recent use and their foreground/background priority state).

By having a running Service in your process, your application has already promoted its priority to be more important than other apps in the system that are in the background, reducing the likelihood that your process gets killed. By making your Service run as foreground, you've only slightly increased this priority level further and it's not the sort of thing Google recommends you do for long periods of time. When the system comes under enough memory pressure that apps start getting killed, eventually your process will suffer the same fate.

If you truly need any of your data to live beyond the life of your process, you need to persist it into any of Android's local storage formats.

Upvotes: 2

Brian Dupuis
Brian Dupuis

Reputation: 8176

The Application object is the longest-lived object in the Android lifecycle. So yes, it has a lifespan at least as long as your Service. I'm unsure how it helps you save anything, though. Once Android kills your process -- and it will at some point, foreground service or no -- you'll still have to reload or recreate your data when your Service is next created.

Upvotes: 0

Related Questions