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

junior

How to persist data in an Android app?

senior

How do you handle Bitmaps in Android as it takes too much memory?

middle

What are the differences between ArrayList and ArrayMap ?

Bình luận

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

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