0

I would like to call getResouces in some classes, however, these classes are not extended to Activity. How to do it in a right way?

Nimantha
  • 6,405
  • 6
  • 28
  • 69
justicepenny
  • 2,004
  • 2
  • 24
  • 48

4 Answers4

2

You must either construct said class with a Context object (Activity extends Context), or pass it into the classes's methods where it's needed. Get used to passing Context around everywhere, that's just the way it is.

Nimantha
  • 6,405
  • 6
  • 28
  • 69
Jeffrey Blattman
  • 22,176
  • 9
  • 79
  • 134
1

You can pass in a pointer to your activity from which you should call getApplicationContext(). In certain cases you would want to use the regular getContext().

bgs
  • 1,210
  • 10
  • 19
1

If passing the Context by parameter is too complicated maybe you are interested in getting the Context statically by using the Application class as described here: Accessing SharedPreferences through static methods

Community
  • 1
  • 1
mreichelt
  • 12,359
  • 6
  • 56
  • 70
-1

Or make a public static Activity variable. On the oncreate set it with the context of the activity running behind and use it.

weakwire
  • 9,284
  • 8
  • 53
  • 78