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

Android Log.v() , Log.d() , Log.i() , Log.w() , Log.e() . When to use each one?

junior

Explain activity lifecycle

middle

What is the difference between Activity and Context ?

Bình luận

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

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