/**
* Return the Context this fragment is currently associated with.
*
* @see #requireContext()
*/
@Nullable
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
현재 fragment에 연결된 context를 반환한다. 연결된 context가 없다면 null을 반환한다.(nullable)
→ 연결된 context는 Activity이다. 즉 프래그먼트가 Activity에 부착된 여부이다.
/**
* Return the Context this fragment is currently associated with.
*
* @throws IllegalStateException if not currently associated with a context.
* @see #getContext()
*/
@NonNull
public final Context requireContext() {
Context context = getContext();
if (context == null) {
throw new IllegalStateException("Fragment " + this + " not attached to a context.");
}
return context;
}
내부적으로 getContext()
메서드를 호출하고 있고, context가 null인 경우에 IllegalStateException
예외를 발생한다. (non-null)
/**
* ㅊ
*
* @throws IllegalStateException if not currently associated with an activity or if associated
* only with a context.
* @see #getActivity()
*/
@NonNull
public final FragmentActivity requireActivity() {
FragmentActivity activity = getActivity();
if (activity == null) {
throw new IllegalStateException("Fragment " + this + " not attached to an activity.");
}
return activity;
}
내부적으로 getActivity()
를 호출하고있고, 현재 프래그먼트와 연결된 Activity를 반환한다.
세 함수 다 fragment가 부착 되어있는 Activity(Context)를 반환한다. 즉, 다 같은 기능을 동작한다. null과 예외를 반환하니 null처리나 예외처리를 사용해서 사용할 것!