It is very easy to get the Context inside an Activity:
- with the getApplicationContext() method
- using only “this” because the Activity class extends the Context class
But you can’t use any of these two methods inside a Fragment and you have to replace them respectively with:
- getActivity().getApplicationContext()
- getActivity()
I often do this replacement when I use code written for an Activity, but that I must use in a Fragment.
Leave a Reply