getContext() in Fragment

22,902

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.

Share:
22,902

Related videos on Youtube

CoolMind
Author by

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, 2020

Comments

  • CoolMind
    CoolMind about 4 years

    Where can I access getContext() in a Fragment? I mean, it is not null and can be used (for instance, for controls creation). Is it onAttach, onCreateView or onActivityCreated?

  • CoolMind
    CoolMind over 6 years
    I think, it's almost the same. I meant that getActivity() or getContext() might be used inside a fragment's lifecycle.
  • CoolMind
    CoolMind almost 6 years
    Are 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
    Mr.O almost 6 years
    Yes 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
    CoolMind almost 6 years
    Agree, this variant is possible.
  • oiyio
    oiyio over 5 years
    in onViewCreated(), context may sometimes be null in my project. so same case may occur in onCreateView(). Thus your assumption is wrong.
  • CoolMind
    CoolMind over 5 years
    I think, we can use isAdded() to determine, whether a fragment still exists. But your variant is better if we rely on context only.
  • CoolMind
    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 before onAttach?
  • oiyio
    oiyio about 4 years
    It sounds logical. The reason of context is null, may be onCreateView is called before onAttach.
  • CoolMind
    CoolMind almost 4 years
    Currently requireContext() and requireActivity() are mandatory instead of getContext() and getActivity(). Your answer is more correct as there are situations when getContext() == null in onCreate() or onCreateView(), according to other topics in StackOverflow.