Best practice to pass Context to non-activity classes?

37,409

Solution 1

You can do that using ContextWrapper, as described here.

For example:

public class MyContextWrapper extends ContextWrapper {

    public MyContextWrapper(Context base) {
      super(base);
   }

    public void makeMyAppAwesome(){
        makeBaconAndEggsWithMeltedCheese(this);
    }
}

And call the non activity class like this from an Activity

new MyContextWrapper(this);

Solution 2

It is usually in your best interest to just pass the current context at the moment it is needed. Storing it in a member variable will likely lead to leaked memory, and start causing issues as you build out more Activities and Services in your app.

public void iNeedContext(Context context) {...

Also, in any class that has context, I'd recommend making a member variable for readability and searchability, rather than directly passing or (ClassName.)this. For example in MainActivity.java:

Context mContext = MainActivity.this;
Activity mActivity = MainActivity.this;
Share:
37,409
Iiro Krankka
Author by

Iiro Krankka

Updated on July 30, 2021

Comments

  • Iiro Krankka
    Iiro Krankka almost 3 years

    So, my first major application is almost coded and I'm doing optimizations on my code. The app works fine, but I'm not sure about my way of passing the context to other classes. I don't want to do it the wrong way. I stumbled upon articles and questions here in Stackoverflow about contexts and which is the right way to pass it to non-activity classes. I read the documentation as well, but being a Finn makes complicated tech speak even harder to understand.

    So, a simple question. Is my way of passing my main activity's context to other (helper) classes correct? If not, where can I read more about better practice on these situations.

    For example: MainActivity.java

    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle sis){
            super(sis);
            new Helper(MyActivity.this).makeMyAppAwesome();
        }
    }
    

    Helper.java

    public class Helper {
        Context context;
        Helper(Context ctx){
            this.context = ctx;
        }
    
        public void makeMyAppAwesome(){
            makeBaconAndEggsWithMeltedCheese(context);
        }
    }
    

    Is this OK? It would be nice if someone could provide an easy to read article with examples on this subject.

  • OJFord
    OJFord almost 10 years
    Welcome to SO, kudos for actually providing a better answer than exists and is accepted on a popular question in your first post! I've edited your answer just to format the code properly - check out the help link for more info next time you answer. :)
  • Jay Donga
    Jay Donga almost 7 years
    public static MainActivity mMainActivity; This will create a global static instance of Activity and will not be garbage collected, hence it will leak memory.
  • Dave Thomas
    Dave Thomas over 6 years
    This is just as bad as retaining the context. Be aware that the ContextWrapper will keep a reference to the context. If you are using this then make sure the ContextWrapper you create is properly dereferenced.
  • Madhan
    Madhan over 3 years
    @DaveThomas I'm having helper class in application level. If I use this approach and pass application context, will it lead to memory leak?
  • Dave Thomas
    Dave Thomas over 3 years
    @Madhan Just pass the context through when it is needed. Never retain it. Any function that needs should accept it as a parameter. See the answer on here detailing that approach.