Nari
Nari

Reputation: 343

Can an Android Context inside an activity be static?

I have a static function in which I need to access my color resources. In order access color resources I need context variable which is static. I am confused if I can make context static. Is there any side effects to it? or, is there any other way I can access my resources without using context

Here is the function

 private static SpannableStringBuilder setTextColor(
      SpannableStringBuilder Text, int spanLength, boolean isSuggestion) {
    addressText.setSpan(
        new ForegroundColorSpan(
            context
                .getResources()
                .getColor(
                    isSuggestion ? R.color.blur: R.color.red)),
        addressText.length() - 1 - spanLength,
        addressText.length(),
        Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    return Text;
  } 

Upvotes: 0

Views: 201

Answers (3)

Son Truong
Son Truong

Reputation: 14173

I am confused if I can make context static. Is there any side effects to it?

You can declare a context as static but it is not recommended in Android, because it might lead to a memory leak in your app.

is there any other way I can access my resources without using context?

No, you need a context instance to access resources in your app.

Back to your case, the easiest way is passing a context as param of the method.

private static SpannableStringBuilder setTextColor(Context context, SpannableStringBuilder Text, int spanLength, boolean isSuggestion) {
    int color = context.getResources().getColor(isSuggestion ? R.color.blur : R.color.red);

    addressText.setSpan(new ForegroundColorSpan(color),
            addressText.length() - 1 - spanLength,
            addressText.length(),
            Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

    return Text;
}

Inside your activity, pass this as context when calling setTextColor, for example.

setTextColor(this, new SpannableStringBuilder(), 0, false); 

Upvotes: 3

Farid Haq
Farid Haq

Reputation: 4199

1) Create your App class which extend Application

private static Context context;

@Override
public void onCreate() {
    super.onCreate();
    context = getApplicationContext();

2) Create getApplicationContext() method which return context

public static Context getApplicationContext() {
  return context;
}

3) Now you can get context anywhere in your class like

Context context = App.getContext().getApplicationContext();

Upvotes: -2

Damon Baker
Damon Baker

Reputation: 907

In Kotlin, you can achieve this by creating a class that extends Application and storing the application context in the companion object.

Usually it will look similar to this:

class App : Application() {

    override fun onCreate() {
        super.onCreate()
        instance = this
    }

    companion object {
        private var instance: App? = null
        val context: Context?
            get() = instance?.applicationContext
    }
}

And you can access the context anywhere via App.context

In regards to your concerns about storing a static context, if you were storing an activity or fragment context then you risk creating memory leaks, but since we're storing the application context which is tied to the lifecycle of the entire application, there won't be any issues with memory leaks.

You may run into issues if you want to write testable code that depends on a static context, in which case I would recommend you pass the context into the function rather than access it directly.

Upvotes: 0

Related Questions