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

senior

What is the difference between ANR and crash in Android?

senior

What is a ThreadPool ? And is it more effective than using several separate Threads ?

entry

What is a difference between Spannable and String ?

Bình luận

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

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