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

前端 未结 8 732
深忆病人
深忆病人 2020-12-02 15:39

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

Do they always return the same obje

相关标签:
8条回答
  • 2020-12-02 15:40

    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.

    0 讨论(0)
  • 2020-12-02 15:41

    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().

    0 讨论(0)
  • 2020-12-02 15:51
    /**
         * 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

    0 讨论(0)
  • 2020-12-02 15:58

    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.

    0 讨论(0)
  • 2020-12-02 15:58

    @mlevytskiy Please check this Stactoverflow Answer. I hope it will helps you.

    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 .

    0 讨论(0)
  • 2020-12-02 15:59

    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.

    0 讨论(0)
提交回复
热议问题