Using context in a fragment

后端 未结 30 2620
Happy的楠姐
Happy的楠姐 2020-11-22 00:05

How can I get the context in a fragment?

I need to use my database whose constructor takes in the context, but getApplicationContext() and Fragmen

30条回答
  •  一生所求
    2020-11-22 00:23

    You have different options:

    • If your minSDK <= 21, then you can use getActivity(), since this is a Context.
    • If your minSDK is >=23, then you can use getContext().

    If you don't need to support old versions then go with getContext().

提交回复
热议问题