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 Doze? What about App Standby?

middle

What are the differences between onCreate() , onCreateView() , and onActivityCreated() in Fragments
and what would they each be used for?

senior

Provide some tips to reduce battery usage in an android application

Bình luận

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

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