Câu hỏi phỏng vấn Android
Câu hỏi

How can I get the context in a Fragment?

Câu trả lời

To get the context in a Fragment, you can use the following methods:

  1. Use the requireContext() method:

    • This method returns the Context associated with the Fragment.
    • It is a non-null version of getContext() and throws an exception if the Fragment is not attached to a Context.
    • Example: Context context = requireContext();
  2. Use the requireActivity() method:

    • This method returns the Activity associated with the Fragment.
    • It is a non-null version of getActivity() and throws an exception if the Fragment is not attached to an Activity.
    • Example: Activity activity = requireActivity();
  3. Use the requireView() method:

    • This method returns the View associated with the Fragment.
    • It is a non-null version of getView() and throws an exception if the Fragment's view is null.
    • Example: View view = requireView();
  4. Use the requireParentFragment() method...

junior

junior

Gợi ý câu hỏi phỏng vấn

middle

What are Retained Fragments?

junior

What is a ContentProvider and what is it typically used for?

senior

What are best practices for storing and protecting private API keys in applications?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào