I have application with a Service. This service starts worker Thread, and this thread needs a pointer to an instance of Service (it calls some methods of this service).
Now i just pass this pointer from Service‘s onCreate() method to my own Runnable constructor, but it works correctly until i close my app. When app is restarted, something becomes really wrong with pointers. I still can’t figure out what especially is wrong, but it seems like pointer to my instance of Service is a pointer to strange another instance of Service, and pointer to my Runnable is wrong too.
If i kill my app, then next time it works correctly, but after restart the same issues is taking place.
I suspect that i can’t pass this pointer from onCreate() method of Service, but then, how can i get pointer to instance of my Service from the thread that starts from onCreate() method?
UPDATE: I just figured out that i have exactly the same singleton issue. The Runnable was a singleton. But i still can’t find any good solution except not to use singletons in Android at all. Maybe, any suggestions?
There are no pointers in Java. The Java equivalent would be “object reference”.
There is no such concept as “closing” an “app” in Android.
That is eminently possible, if you stopped the original
Serviceand leaked the thread, then started up theServiceagain. You may well have wound up with two threads, one holding a defunct copy of yourService.That is not your problem. Your problem, whatever it is, lies elsewhere.