Needing Context in non-Activity classes

59,726

Solution 1

It depends on the role of the class. But anyway pass ApplicationContext but not Activity one. If you pass Activity context gc can't remove it from the memory when after you don't need activity anymore. But application context is used while application was not finished by OS.Refer Avoid Memory Leaks

Solution 2

Pass it as a parameter. Or better yet, get the application context to avoid memory leaks.

public class Example {
    protected Context context;

    public Example(Context context){
        this.context = context.getApplicationContext();
    }
}

Solution 3

I'm pretty much always going with a constructor parameter approach. I pass it in the instantiation and keep a private reference in the instantiated class.

You have to think about one important thing. If the class you pass the Context will exist longer than the Activity instantiating it then you should use the application context. If that class is doing UI stuff you will need an activity context.

Make sure that the class you are passing an activity context to won't last longer than the Activity or you'll leak the entire activity.

If you don't do UI stuff then go with the application context.

Solution 4

I pass it as a parameter, i think its de best form to do it

Solution 5

Pass it at class instantiation and keep it.

One typical example is when you create a db helper. See this link

Share:
59,726

Related videos on Youtube

theblitz
Author by

theblitz

Updated on June 12, 2020

Comments

  • theblitz
    theblitz about 4 years

    I have some classes within my application that need to call Android functions that require the Context as a parameter. I don't have it as the class is not a subclass of the Activity class.

    What is the correct way to tackle this problem?

    1. Pass it as a parameter on each call?
    2. Pass it at class instantiation and keep it?
  • theblitz
    theblitz over 13 years
    On each call to the function?
  • theblitz
    theblitz over 13 years
    Perfect!Just what I was looking for
  • Aracem
    Aracem over 13 years
    Depends on the case. If u need only in a methop use a parameter. If u need in a class where it use many times, pass a parameter in the constructor and asing to a atribute: public class Example{ private Context mContext; public Example(Context context,int other, int other,int other){ this.context = context; [...] } public void method(){ Toast.makeText(mContext,"text",Toast.Toast.LENGTH_SHORT).sho‌​w(); } }
  • theblitz
    theblitz over 13 years
    It is a UI application so the instantiated class will disappear on exit.
  • Octavian Damiean
    Octavian Damiean over 13 years
    Well I know that it is an UI application as you were talking about an Activity but is the class needing that context doing UI stuff?
  • theblitz
    theblitz about 13 years
    :) Nope. But it is going to need to access SQLite so it needs the Context.
  • theblitz
    theblitz about 13 years
    Won't it disappear with the activity?
  • Octavian Damiean
    Octavian Damiean about 13 years
    Right then you should go for a non activity context.
  • theblitz
    theblitz about 13 years
    So - the Application Context?
  • Maxim
    Maxim about 13 years
    @theblitz If context object is used it is not be collected (as any other object)
  • theblitz
    theblitz about 13 years
    I meant that surely my instantiated class will disappear with the Activity itself when it closes.
  • theblitz
    theblitz about 13 years
    So just pass this.getApplicationContext to any class that need it. Right?
  • Octavian Damiean
    Octavian Damiean about 13 years
    Well in that particular case yes but keep in mind what I posted in my answer. With that type of context you won't always be able to do what you want.
  • theblitz
    theblitz about 13 years
    All my UI happens at the top level so it shouldn't be a problem.
  • theblitz
    theblitz about 13 years
    How come they pass "this" to NotesDbAdapter in the Notepad example? Isn't that the same thing?
  • Maxim
    Maxim about 13 years
    It is the same thing. So their example is not match to idea of their article I mentioned above. Same thing you can meet with convertView of CustomAdapter getView method that explained by Romain Guy on Google I/O. But convertView isn't used in some developer.android.com articles
  • Adam Matan
    Adam Matan almost 12 years
    Link's dead - any new references?