Eugene
Eugene

Reputation: 60184

Is it safe to use reference to the Application where Context is required?

As it stands we need to pass a Context in many places within Android application. I'm wondering how safe it is to use a static variable which refers to the Application class instance in such places? For example, I have a static method in one of my Activities and I can't use this (as Activity) or something because the method is static, so I'm guessing to use the Application.instance reference which is initialized on each application start. Is it ok you think? Thanks a lot.

Upvotes: 2

Views: 60

Answers (1)

vRallev
vRallev

Reputation: 5040

It depends on your method and what you are trying to do with the Context. If you want to get resources like a String or Drawable from your app, then it is save to use the Application Context. But if you want to display a Dialog, then you definitely should use the Activity Context.

As far as I know, for a Toast you can use the Application Context as well.

Upvotes: 3

Related Questions