I've got a Java object which uses a singleton (e.g. somewhere in the object's code is Singleton.getInstance()
). I'm switching over to using dependency injection (Dagger 2), and the standard way to do it would be to create a factory for this class, have the DI framework inject the singleton into the factory, and have the factory pass the singleton to the object's constructor (along with the other arguments), which would store it in a member variable.
But this object is serializable. Marking the member variable containing the singleton transient
allows serialization to happen, but when the object is deserialized the member variable is null
. What's the best way to get the dependency again after deserialization?