getContext() in Fragment
Solution 1
All the mentioned answers are basically correct. You should get the activity's context between onAttach and onDetach so I like adding this to my fragments:
private Context mContext;
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
@Override
public void onDetach() {
super.onDetach();
mContext = null;
}
and then whenever I use mContext I add a check:
if(mContext != null) {
//your code that uses Context
}
UPDATE:
In Support Library 27.1.0 and later, Google has introduced new methods requireContext() and requireActivity() that will return a non null Context or Activty.
If the Fragment is not currently attached at the time of calling the method, it will throw an IllegalStateException: so use within a try catch block.
Solution 2
getContext()
will always be not null between onAttach()
and onDetach()
Solution 3
Use getActivity()
between onAttach
and onDetach
to get the attached Activity
which is the Context
of the Fragment
.
Solution 4
You're pretty safe by calling getContext()
inside onCreateView()
. If you take a look at the docs you'll see that from onAttach
onwards your fragment will have a context.
Related videos on Youtube
![CoolMind](https://i.stack.imgur.com/b8BDa.jpg?s=256&g=1)
CoolMind
After several years of work with VB/VBA, MS Office, SQL Server I changed a direction and started to learn Android and Java. Currently I use Kotlin in new projects. I think it has shorter syntax and safer than Java. Like to travel in Russia.
Updated on April 16, 2020Comments
-
CoolMind about 4 years
Where can I access
getContext()
in aFragment
? I mean, it is not null and can be used (for instance, for controls creation). Is itonAttach
,onCreateView
oronActivityCreated
? -
CoolMind over 6 yearsI think, it's almost the same. I meant that
getActivity()
orgetContext()
might be used inside a fragment's lifecycle. -
CoolMind almost 6 yearsAre there cases when mContext is null and a fragment still exists? Or do you mean there is a long-running operation that ends when fragment is detached?
-
Mr.O almost 6 yearsYes if there's a long running operation with a callback in the fragment and you've just clicked on a button that switches fragments, so your fragment is no longer attached to your activity.
-
CoolMind almost 6 yearsAgree, this variant is possible.
-
oiyio over 5 yearsin onViewCreated(), context may sometimes be null in my project. so same case may occur in onCreateView(). Thus your assumption is wrong.
-
CoolMind over 5 yearsI think, we can use
isAdded()
to determine, whether a fragment still exists. But your variant is better if we rely on context only. -
CoolMind about 4 years@oiyio, it is interesting. I read in some question, that they really had gotten context == null. I understand so, that when a fragment recreates it can be created before an activity. So, can it mean that
onCreateView
is called beforeonAttach
? -
oiyio about 4 yearsIt sounds logical. The reason of context is null, may be onCreateView is called before onAttach.
-
CoolMind almost 4 yearsCurrently
requireContext()
andrequireActivity()
are mandatory instead ofgetContext()
andgetActivity()
. Your answer is more correct as there are situations whengetContext() == null
inonCreate()
oronCreateView()
, according to other topics in StackOverflow.