49

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?
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
theblitz
  • 6,683
  • 16
  • 60
  • 114

7 Answers7

34

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

nothrow
  • 15,882
  • 9
  • 57
  • 104
Maxim
  • 2,996
  • 17
  • 19
  • Won't it disappear with the activity? – theblitz Mar 31 '11 at 13:15
  • @theblitz If context object is used it is not be collected (as any other object) – Maxim Mar 31 '11 at 13:21
  • I meant that surely my instantiated class will disappear with the Activity itself when it closes. – theblitz Mar 31 '11 at 13:27
  • How come they pass "this" to NotesDbAdapter in the Notepad example? Isn't that the same thing? – theblitz Mar 31 '11 at 15:13
  • 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 – Maxim Mar 31 '11 at 15:17
  • 1
    Link's dead - any new references? – Adam Matan Sep 20 '12 at 06:18
19

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();
    }
}
Mark Mooibroek
  • 7,636
  • 3
  • 32
  • 53
10

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.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
3

I've answered this question here also.

You can do that using ContextWrapper, as described here.

For example:

public class MyContextWrapper extends ContextWrapper {

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

}

and use that class as it were Context

Community
  • 1
  • 1
ET-CS
  • 6,334
  • 5
  • 43
  • 73
3

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

Aracem
  • 7,227
  • 4
  • 35
  • 72
  • On each call to the function? – theblitz Mar 31 '11 at 11:00
  • 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).show(); } }` – Aracem Mar 31 '11 at 11:11
3

Pass it at class instantiation and keep it.

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

ccheneson
  • 49,072
  • 8
  • 63
  • 68
1

The best way is to follow Bean approach:

public class Example {
    protected Context getContext() {
        ...
    }

    ...
}

Then it depends on possibilities to access context. If class is fully independent then constructor parameter and private field seems best approach.

But that bean property way shields you from further code changes.

evilcroco
  • 521
  • 4
  • 8