0

I've been looking into Android development using Kotlin and I ran into a situation. I'm trying to figure out how to create a singleton using the recommended Kotlin "object" and pass it an Android Context, more specifically the context from the Context.getApplicationContext() method.

It's essentially the same question as here, but since Kotlin was in beta until just a few days ago, I was wondering if the answer might have changed for V1.0.0.

I'd rather not use the accepted answer in the referred question (use a setter to set the context) because it would mean I'd have to (re)set the context to make sure it's not null before using the singleton. Not a big deal, but it feels like there should be a better way. It might also raise issues when setting the context from different threads at the same time...? Or maybe not, since the Context would always be the same Application Context.

Using an injection library as suggested in the other answer to that question would work, but I'm looking for a more "idiomatic" way of doing this since I'm trying to learn the language.

Anyone know another way?

Thanks!

Community
  • 1
  • 1
Danilo Carvalho
  • 395
  • 2
  • 12
  • 1
    Kotlin has not changed substantially since that question you referenced was written. – Doug Stevenson Feb 17 '16 at 06:11
  • @DougStevenson thanks, man. Do you know another way of doing this other than the two answers I mentioned? – Danilo Carvalho Feb 17 '16 at 15:18
  • @nhaarman I understand this question has been asked before, but I do say in my question that the answers provided there don't completely satisfy my requirements. I would have appreciated if my question had stayed at least a few days to see if anyone else could provide a different answer. – Danilo Carvalho Feb 17 '16 at 15:20
  • There are no substantial changes since that question, as Doug mentioned. The linked question and answer acknowledges the limitation and better answers can still be given there - like you said, it's the same question. – nhaarman Feb 17 '16 at 15:38
  • Idiomatic is what is in the other answers, there is no new magic answer. You cannot inject yourself into the lifecycle of an `Object` instance being created, you can only inject a value into its members as they are initialized using something that can "grab" the value (i.e. the injection library answer) – Jayson Minard Feb 17 '16 at 20:19
  • 1
    For an actual answer to your question, in your application subclass: override fun onCreate(){ super.onCreate() context = this } companion object{ lateinit var context: Context private set } Then you can just do App.context wherever you want the application context ***** edit sorry about the formatting. This question is flagged as a duplicate, and really shouldn't be, so I can't post it as an actual answer. – Jim Pekarek May 06 '16 at 20:03
  • 1
    @Amagi82 thanks, I ended up just using a regular class for the singleton (instead of a Kotlin object) and doing the instance checking myself, so I just pass the context to the "getInstance()" companion method. But your suggestion is very interesting. Will try that. And yeah, I specifically say that I don't want to use the answer to the "duplicate" question but they marked it as duplicate anyway and only 2 hours after I posted my question. They could've at least waited a few days to see if anyone came up with an alternative. – Danilo Carvalho May 17 '16 at 23:52

0 Answers0