-1

I have a Three choice to use context in fragment. The first one is using Fragment(context : Context). I think this way is not good because of dependency. The second is using getContext. In java I couldn't use getContext(),but In Kotlin I have known using context(getContext()) is possible(I don't understand why this is possible). And The Third is using GlobalApplicationContext. Which is the best way?

Charles
  • 181
  • 1
  • 14

2 Answers2

1

Use getActivity(), which returns the activity associated with fragment. The activity is a context (since its extend context) and also using GlobalAppContext is not recommended

HameedR
  • 43
  • 1
  • 6
  • 1
    And in Kotlin you can also use `requireActivitty()` or `requireContext()` to return a non-null version (instead these will throw an exception when the Fragment is not currently attached to an Activity). – RobCo May 19 '20 at 08:21
0

view.context This is the view returned from onCreateView method.

Hritik Gupta
  • 611
  • 5
  • 20