728x90
따라가 보자, 왜그런가...
requireActivity() 본체는 아래와 같으며 결국 getActivity() 에서 null 이다.
/**
* Return the {@link FragmentActivity} this fragment is currently associated with.
*
* @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() 본체를 보자
mHost check필요.
/**
* Return the {@link FragmentActivity} this fragment is currently associated with.
* May return {@code null} if the fragment is associated with a {@link Context}
* instead.
*
* @see #requireActivity()
*/
@Nullable
final public FragmentActivity getActivity() {
return mHost == null ? null : (FragmentActivity) mHost.getActivity();
}
isAdded 로 재확인 처리 필요.
/**
* Return true if the fragment is currently added to its activity.
*/
final public boolean isAdded() {
return mHost != null && mAdded;
}
결국. 문제가 발생하는 코드 수행전 아래의 코드로 방어처리하면될듯.
if (requireActivity() == null) {
return;
}
if (!isAdded()) {
return;
}
// do something.
728x90
'Android' 카테고리의 다른 글
[Android] Kotlin Chronometer (0) | 2023.08.29 |
---|---|
[Android] Kotlin View binding (0) | 2023.08.29 |
[Android] CameraX (0) | 2023.08.04 |
[Android] Integration OpenCv (0) | 2023.08.04 |
[Android] android 13-14 대응 RECEIVER_NOT_EXPORTED (0) | 2023.08.04 |