Fragment 的 getContext 和传递给 Fragment 的 onCreateView 的容器的 getContext 有区别吗?

Is there a difference between getContext of Fragment and getContext of container passed to onCreateView of Fragment?

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)

说如果我们打电话

fragment.getContext()container.getContext 我们会得到相同的结果吗,主机 activity?

view.getContext() 总是 return s Activity context 还是 return 其他类型的 context?

Activity class in android extends Context class. So basically an activity is a context. But a context may not be an activity.

fragment.getContext() 将 return 它附加到的容器的上下文。所以是的 container.getContext() 将具有与 fragment.getContext() 相同的结果,即主机 activity.

When instance of the ViewGroup is created, the inflater pass context of that activity to it. Means container.getContext() will return that same context.

fragment.getActivity() 将 return 附加一个 activity ,它又是一个上下文。当片段从 activity 分离时,它 return 为空。附加时它也 returns 与 getContext()

相同

关于你的最后一个问题,view.getContext() return它所在的上下文 运行。 View 不扩展 Context class,事实上,当它们被创建时,它们需要 context 对象作为参数。因此,当您在 Activty 中创建视图时,您需要传递 Context。当您调用 view.getContext() 时,您将获得与创建它时传递的上下文相同的上下文。