What is the difference between this, getContext() and getActivity()?

后端 未结 2 1553
不思量自难忘°
不思量自难忘° 2020-12-13 04:23

I am very confused with the usage of all these that where should we use them.

2条回答
  •  猫巷女王i
    2020-12-13 04:47

    Activity is a subclass of Context, so whenever a context is required, either can be given.

    getActivity() is at least a method on Fragment, to get the activity it is attached to.

    Whenever a context is needed in an instance method of an activity, you can use this.

    A context is needed whenever contextual info is needed, or when stuff needs to be displayed.

提交回复
热议问题