기본 콘텐츠로 건너뛰기

9월, 2017의 게시물 표시

Fragment의 inInLayout()에 대하여

activity의 onConfigurationChanged() 콜벡 발생시 (orientation변경시) 이전 상태의 fragment instance를 케싱한 것을 알아내는 법을 구글 개발자 문서를 통해서 알아내었다. 바로 fragment의 isInLayout()메소드를 사용하는 것이다. isInLayout()메소드에 대한 설명은 다음과 같다. When a configuration change causes the activity hosting these fragments to restart, its new instance may use a different layout that doesn't include the same fragments as the previous layout. In this case all of the previous fragments will still be instantiated and running in the new instance. However, any that are no longer associated with a <fragment> tag in the view hierarchy will not have their content view created and will return false from  isInLayout() 이러한 메소드의 기능은, activity의 orientation이 변경되어 재생성되고난 후에 새롭게 생선된 acticvity의 레이아웃이 더이상 <fragment>태그로 가지지 않는 fragment를 FragmentManager로 fragment를 참조하려 할때, 해당 참조가 back stack에 있는 fragment의 참조가 리턴된 경우를 처리해 줄때 유용하다. back stack의 참조라면 현재 레이아웃에서는 존재하지 않아서 isInLayout()이 false를 반환한다. 따라서 이런 경우를 조건문으로 걸러주어 현재 layout에 fragment가 존재하지 않는