728x90
context
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @see #requireContext()
*/
@Nullable
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
requireContext
/**
* Return the {@link 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;
}
둘 다 return 하는 값은 동일하다.
requireContext 의 특징
- requireContext
- 현재 context 가 null 일 경우 Exception 발생.
- context 가 null 이 아닐 경우가 확실할 때 사용 혹은 사용할 때 throw 로 Exception 처리
728x90
'Android > 개념 및 정보' 카테고리의 다른 글
[Android] MVVM 의 ViewModel (using LiveData) (0) | 2020.12.31 |
---|---|
[Android] Hilt 의존성 주입(DI) gradle 버전, 각 어노테이션 설명 (0) | 2020.12.11 |
[Android] Room Database 튜토리얼 + MVVM + Repository(with kotlin) (0) | 2020.12.08 |
[Android] Realm Database 첫 시작(with kotlin) (2) | 2020.12.05 |
Android Studio 4.1 업데이트, 새로운 기능과 변경사항 (2) | 2020.10.18 |
댓글