What is different between getContext()
and getActivity()
from Fragment
in support library?
Do they always return the same obje
You can use getActivity()
, which returns the activity associated with a fragment. The activity is a context (since Activity extends Context).
getActivity()
can return null if it is called before onAttach of the respective fragment. Context provides information about the Actvity or Application to newly created components. Relevant Context should be provided to newly created components (whether application context or activity context). Since Activity is a subclass of Context, one can use this to get that activity's context.
getContext()
Returns the context view only current running activity.
getContext() :
Returns the context view only current running activity.
getActivity():
Return the Activity this fragment is currently associated with.