NavigationComponent를 사용하면서 화면이동 시 종료되는 현상이 있어서 원인을 분석하려고 해당 클래스를 봤는데

java.lang.IllegalArgumentException: No view found for id 0x14a3f13b (unknown) for fragment SimplePictureFragment{98c4359} (accc6e9b-5142-4e72-8958-83bcf6fff324) id=0x14a3f13b}

 

FragmentStatePagerAdapter 가 Deprecated 되었습니다. 유용하게 잘 사용해오던 클래스들이 항상 Deprecated 되어서 힘들기도 하지만 어쩌겠습니까.. 오류를 수정하기 전에 새로 적용해보기로했습니다.

 /** <p>This version of the pager is more useful when there are a large number
 * of pages, working more like a list view.  When pages are not visible to
 * the user, their entire fragment may be destroyed, only keeping the saved
 * state of that fragment.  This allows the pager to hold on to much less
 * memory associated with each visited page as compared to
 * {@link FragmentPagerAdapter} at the cost of potentially more overhead when
 * switching between pages.
 * /

 

ViewPager2 와 FragmentStateAdapter로 적용하라고 나옵니다. 어떤이유로 변경해야하며 변경하게되면 어떤점이 좋은지 찾아봐야하지만 쉽지 않습니다.

 

적용은 그리 어렵지 않습니다.

- 기존에 FragmentStatePagerAdapter 상속받았던 아답터를 FragmentStateAdapter로 변경하면 됩니다.

- xml에서 ViewPager을 ViewPager2로 변경합니다.

 

그리고 탭레이아웃과 연동하는 부분도 변경되었습니다. 아래와 같이 적용해주니 동작했습니다.

// itemTimeLineBinding.include.tl.setupWithViewPager(itemTimeLineBinding.viewpager)
TabLayoutMediator(itemTimeLineBinding.include.tl, itemTimeLineBinding.viewpager){ tab, position ->
}.attach()

 

아래와 같은 오류도 발생하였는데 탭레이아웃과 뷰페이저를 연결하기 전에 adapter가 셋팅이 되어야합니다,

TabLayoutMediator attached before ViewPager2 has an adapter

 

+ Recent posts