How can I get the context in a Fragment?
How can I get the context in a Fragment?
To get the context in a Fragment, you can use the following methods:
Use the requireContext()
method:
getContext()
and throws an exception if the Fragment is not attached to a Context.Context context = requireContext();
Use the requireActivity()
method:
getActivity()
and throws an exception if the Fragment is not attached to an Activity.Activity activity = requireActivity();
Use the requireView()
method:
getView()
and throws an exception if the Fragment's view is null.View view = requireView();
Use the requireParentFragment()
method...
junior
Gợi ý câu hỏi phỏng vấn
Chưa có bình luận nào