Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sharing singleton java class by Service and Activity

Is there a potential issue using a Singleton Java class by both a Service and Activity in an Android application?

Example: Singleton is Singleton Activity is A Service S

Service S has a handle to Singleton. S requests Singleton to launch Activity. Activity calls operational methods defined in Singleton.

I have several Singletons in my application which encapsulate various operations for a functional area. So Activities will interact with various Singletons to perform various operations.

like image 774
and-dev Avatar asked Dec 03 '25 12:12

and-dev


1 Answers

I don't think there is an issue with this approach. I use singletons quite often and never had any problems. You just need to keep in mind that android might remove the singleton instance when it needs the memory, so you need to check for null every time you get the singleton instance.

like image 169
SimonSays Avatar answered Dec 06 '25 00:12

SimonSays



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!