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?
Asked
Active
Viewed 128 times
4 Answers
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
- 5,793
- 5
- 23
- 56
Jeffrey Blattman
- 21,781
- 9
- 77
- 132
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,180
- 9
- 18
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
-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,249
- 8
- 52
- 78