개발/안드로이드

FragmentPagerAdapter와 FragmentStatePagerAdapter의 차이점

맨날치킨 2022. 12. 30. 21:05
반응형

Stack Overflow에 자주 검색, 등록되는 문제들과 제가 개발 중 찾아 본 문제들 중에서 나중에도 찾아 볼 것 같은 문제들을 정리하고 있습니다.

Stack Overflow에서 가장 먼저 확인하게 되는 가장 높은 점수를 받은 Solution과 현 시점에 도움이 될 수 있는 가장 최근에 업데이트(최소 점수 확보)된 Solution을 각각 정리하였습니다.

 

아래 word cloud를 통해 이번 포스팅의 주요 키워드를 미리 확인하세요.

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

FragmentPagerAdapter와 FragmentStatePagerAdapter의 차이점은 무엇인가요?

 문제 내용 

What is the difference between FragmentPagerAdapter and FragmentStatePagerAdapter?

FragmentPagerAdapter와 FragmentStatePagerAdapter의 차이점은 무엇인가요?

 

About FragmentPagerAdapter Google's guide says:

FragmentPagerAdapter에 대해서 구글 가이드에서는 다음과 같이 설명하고 있습니다:

 

This version of the pager is best for use when there are a handful of typically more static fragments to be paged through, such as a set of tabs. The fragment of each page the user visits will be kept in memory, though its view hierarchy may be destroyed when not visible. This can result in using a significant amount of memory since fragment instances can hold on to an arbitrary amount of state. For larger sets of pages, consider FragmentStatePagerAdapter.

이 Pager의 버전은 탭과 같이 일반적으로 정적인 몇 개의 Fragment를 페이지로 나열할 때 가장 적합합니다. 사용자가 방문한 각 페이지의 Fragment는 메모리에 유지되지만, 그 Fragment의 뷰 계층 구조는 보이지 않을 때 파괴될 수 있습니다. 이는 Fragment 인스턴스가 임의의 상태 양을 유지할 수 있기 때문에 상당한 메모리 사용량을 초래할 수 있습니다. 페이지 수가 많은 경우 FragmentStatePagerAdapter를 고려해보십시오.

 

And about FragmentStatePagerAdapter:

FragmentStatePagerAdapter 에 대해 설명하자면,

 

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 FragmentPagerAdapter at the cost of potentially more overhead when switching between pages.

 FragmentStatePagerAdapter를 고려하십시오. 이 페이저 버전은 리스트 뷰처럼 대량의 페이지가 있는 경우 더 유용합니다. 사용자에게 보이지 않을 때 페이지의 전체 프래그먼트가 파괴될 수 있으며 해당 프래그먼트의 저장된 상태만 유지합니다. 이렇게 함으로써, 각 방문한 페이지와 관련된 메모리가 FragmentPagerAdapter와 비교하여 훨씬 적게 유지될 수 있지만 페이지 간 전환 시 더 많은 오버헤드가 발생할 수 있습니다.

 

So I have just 3 fragments. But all of them are separate modules with a large amount of data.

저는 지금 3개의 프래그먼트를 가지고 있습니다. 그러나 이들은 모두 큰 양의 데이터를 가진 별개의 모듈입니다.

 

Fragment1 handles some data (which users enter) and passes it via activity into Fragment2, which is just a simple ListFragment. Fragment3 is also a ListFragment.

Fragment1은 사용자가 입력하는 일부 데이터를 처리하고, 해당 데이터를 액티비티를 통해 간단한 ListFragment인 Fragment2로 전달합니다. Fragment3도 ListFragment입니다.

 

So my questions are: Which adapter should I use? FragmentPagerAdapter or FragmentStatePagerAdapter?

그렇다면 제가 사용해야 할 어댑터는 무엇일까요? FragmentPagerAdapter와 FragmentStatePagerAdapter 중 어느 것을 사용해야 할까요?

 

 

 

 높은 점수를 받은 Solution 

Like the docs say, think about it this way. If you were to do an application like a book reader, you will not want to load all the fragments into memory at once. You would like to load and destroy Fragments as the user reads. In this case you will use FragmentStatePagerAdapter. If you are just displaying 3 "tabs" that do not contain a lot of heavy data (like Bitmaps), then FragmentPagerAdapter might suit you well. Also, keep in mind that ViewPager by default will load 3 fragments into memory. The first Adapter you mention might destroy View hierarchy and re load it when needed, the second Adapter only saves the state of the Fragment and completely destroys it, if the user then comes back to that page, the state is retrieved.

문서에서 설명한 대로 생각해보세요. 책 리더와 같은 애플리케이션을 만들 경우 사용자가 읽는 동안 모든 프래그먼트를 한꺼번에 메모리에 로드하지 않습니다. 사용자가 읽으면서 프래그먼트를 로드하고 파괴하길 원할 것입니다. 이 경우 FragmentStatePagerAdapter를 사용할 것입니다. 만약 매우 많은 데이터(비트맵과 같은)를 포함하지 않는 3개의 "탭"만 표시하는 경우 FragmentPagerAdapter를 사용하는 것이 적합할 것입니다. 또한, 기본적으로 ViewPager는 3개의 프래그먼트를 메모리에 로드합니다. 첫 번째 언급한 어댑터는 뷰 계층 구조를 파괴하고 필요할 때마다 다시 로드할 수 있지만, 두 번째 어댑터는 프래그먼트의 상태만 저장하고 완전히 파괴하며, 사용자가 해당 페이지로 돌아오면 상태를 검색합니다.

 

 

 

 가장 최근 달린 Solution 

FragmentPagerAdapter: the fragment of each page the user visits will be stored in memory, although the view will be destroyed. So when the page is visible again, the view will be recreated but the fragment instance is not recreated. This can result in a significant amount of memory being used. FragmentPagerAdapter should be used when we need to store the whole fragment in memory. FragmentPagerAdapter calls detach(Fragment) on the transaction instead of remove(Fragment).

FragmentPagerAdapter: 사용자가 방문하는 각 페이지의 Fragment는 메모리에 저장되지만 뷰는 파괴됩니다. 그래서 페이지가 다시 표시될 때, 뷰는 다시 만들어지지만 Fragment 인스턴스는 재생성되지 않습니다. 이로 인해 상당량의 메모리가 사용될 수 있습니다. Fragment 전체를 메모리에 저장해야 하는 경우 FragmentPagerAdapter를 사용해야 합니다. FragmentPagerAdapter는 remove(Fragment) 대신 detach(Fragment)를 호출합니다.

 

 

 

출처 : https://stackoverflow.com/questions/18747975/what-is-the-difference-between-fragmentpageradapter-and-fragmentstatepageradapte

반응형