The Volley documentation recommends implementing a...
# android
c
The Volley documentation recommends implementing a Singleton so the RequestQueue would be retained throughout the app. Of course it shows it in Java. While the J code is kind of bizarre (the class is called MySingleton), it is pretty simple. It can be found here: https://developer.android.com/training/volley/requestqueue.html. I did find a post on medium about implementing Singleton in Kotlin when you have an argument. Seemed like a LOT of work/mess. Anyway, is that the best solution? to just do it with the approach described here: https://medium.com/@BladeCoder/kotlin-singletons-with-argument-194ef06edd9e
g
I have feelings that singleton with arguments is anti-pattern, especially if you use it like LocalBroadcastManager. Much better to provide instance using DI and just construct it once, if you want. Because “save the first context” approach is really hacky thing. If you don’t want to use DI just use lazy field in your App instance or something similar. Of course singleton with params used not only for Android and Context, but still I think that much better to avoid it. And it’s good that the language doesn’t allow you to use such pattern.
Example with database in this article looks really bad for me, much better to use DI instead
c
Yeah I agree, but the problem is the god object is everywhere in Android, you’re gonna get stuck with this at some point. I was mostly bummed that the beauty and simplicity of Singletons in Kotlin goes straight out the window thanks to the need for Context inside. As to DI, in essence what you are saying is I just want the sausage to appear so I don’t have to think about how it’s being made, no?