What is different between getContext and getActivity from Fragment in support library?

Android

Android Problem Overview


What is different between getContext() and getActivity() from Fragment in support library?

Do they always return the same object? (activity associated with current fragment)

Android Solutions


Solution 1 - Android

In most cases there is no difference but ...

So originally Fragments were hosted in FragmentsActivity and back then to get Context one called getActivity().

Just checked the sources and Fragments now can be hosted by anyone implementing FragmentHostCallback interface. And this changed in Support Library version 23, I think.

When using newer version of Support Library, when Fragment is not hosted by an Activity you can get different objects when calling getActivity() and getContext().

When you call getActivity() you get an Activity which is a Context as well. But when you call getContext you will get a Context which might not be an Activity.

Solution 2 - Android

So far, the only provided implementation of FragmentHostCallback (in the OS and the support library) always returns the same value for both getContext() and getActivity().

However, the other constructors of FragmentHostCallback suggest that in future implementations, we may get:

  • A null Activity and a non-null Context which is not an Activity. This looks improbable but we can imagine that fragments could be used outside Activities in the future, or be fully sandboxed.
  • A non-null Activity and a non-null Context which is not the same instance as the Activity. For example, Context could be a ContextThemeWrapper.

Conclusion: when you can, use getContext(). When you need Activity-specific calls, use getActivity().

Solution 3 - Android

Activity is a subclass of Context. Activity has also Window elements and access to UI methods, Context doesn't. However, in the majority of the cases, it's the same if you need only the Context.

Solution 4 - Android

getContext():- Returns the context the view is currently running in. Usually the currently active Activity. getContext() is not defined in an Activity. It's used in a View (or View subclass) to get a reference to the enclosing context (an Activity).

getActivity():- This method gives the context of the Activity. You can use it is like the yourActivity.this. getActivity() is normally used in fragments to get the context of the activity in which they are inserted or inflated.

Solution 5 - Android

getContext() - Returns the context view only current running activity.

getActivity()- Return the Activity this fragment is currently associated with.

getActivity() can be used in a Fragment for getting the parent Activity of the Fragment .

Solution 6 - Android

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.

Solution 7 - Android

getContext() :

> Returns the context view only current running activity.

getActivity():

> Return the Activity this fragment is currently associated with.

Solution 8 - Android

/**
     * Return the {@link Context} this fragment is currently associated with.
     */
    public Context getContext() {
        return mHost == null ? null : mHost.getContext();
    }

    /**
     * Return the {@link FragmentActivity} this fragment is currently associated with.
     * May return {@code null} if the fragment is associated with a {@link Context}
     * instead.
     */
    final public FragmentActivity getActivity() {
        return mHost == null ? null : (FragmentActivity) mHost.getActivity();
    }

from source code, we can find that when a fragment is attached to an Activity, getContext returns null. While getActivity returns null when a fragment is attached to context instead

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionmlevytskiyView Question on Stackoverflow
Solution 1 - AndroidpelotasplusView Answer on Stackoverflow
Solution 2 - AndroidBladeCoderView Answer on Stackoverflow
Solution 3 - AndroidFilnikView Answer on Stackoverflow
Solution 4 - AndroidJay DwivediView Answer on Stackoverflow
Solution 5 - AndroidIntelliJ AmiyaView Answer on Stackoverflow
Solution 6 - Androiduser2315314View Answer on Stackoverflow
Solution 7 - AndroidRamiView Answer on Stackoverflow
Solution 8 - Androidray liuView Answer on Stackoverflow