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

前端 未结 8 733
深忆病人
深忆病人 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 16:03

    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.

    0 讨论(0)
  • 2020-12-02 16:05

    getContext() :

    Returns the context view only current running activity.

    getActivity():

    Return the Activity this fragment is currently associated with.

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