How can I get the context in a fragment?
I need to use my database whose constructor takes in the context, but getApplicationContext()
and Fragmen
The simple way is to use getActivity()
. But I think the major confusion of using the getActivity()
method to get the context here is a null pointer exception.
For this, first check with the isAdded()
method which will determine whether it's added or not, and then we can use the getActivity()
to get the context of Activity.