반응형
프래그먼트를 포커싱 하면 구성한 로직에 따라 비동기적으로 데이터를 호출하고 결과가 오면 해당 프래그먼트의 View를 업데이트하는 로직이 있는데, API 호출하여 데이터 수신된 이후에 해당 View가 더 이상 유효하지 않아서 발생하는 문제로 보입니다.
따라서 아래와 같이 코드를 처리했습니다.
GetInfoApiTask().executeAsync({ result ->
val isValidViewStatus = isAdded && !isDetached && context != null
if (!isValidViewStatus) return@executeAsync
세 가지 조건을 체크해줬습니다.
/**
* Return true if the fragment is currently added to its activity.
*/
final public boolean isAdded() {
return mHost != null && mAdded;
}
프래그먼트가 현재 Activity에 붙어있는 상태인지 확인
/**
* Return true if the fragment has been explicitly detached from the UI.
* That is, {@link FragmentTransaction#detach(Fragment)
* FragmentTransaction.detach(Fragment)} has been used on it.
*/
final public boolean isDetached() {
return mDetached;
}
프래그먼트가 현재 UI 계층에서 떨어져 나간 상태인지 확인
/**
* Return the {@link Context} this fragment is currently associated with.
*
* @see #requireContext()
*/
@Nullable
public Context getContext() {
return mHost == null ? null : mHost.getContext();
}
현재 프래그먼트 자체 Context가 유효한지 확인
반응형