https://kotlinlang.org logo
#android
Title
# android
c

Chung

03/26/2020, 1:57 AM
I am learning sunflower app and came across this
tabFragmentsCreators
variable https://github.com/android/sunflower/blob/master/app/src/main/java/com/google/samples/apps/sunflower/adapters/SunflowerPagerAdapter.kt#L32 What is the benefit of using a
Map<Int, () -> Fragment>
instead of just
Map<Int, Fragment>
a

Araib

03/26/2020, 6:05 AM
Lazy creating fragment instance instead of keeping fragment instance in map (that's my understanding of it..)
c

Chung

03/26/2020, 6:06 AM
Thanks, so it is just creating the fragments when it is "asked"(on-demand)?
a

Araib

03/26/2020, 6:08 AM
Yeah
Copy code
() -> fragment
Will provide fragment when required
So instead of keeping instance of fragment mapped to int, we keep a function which gives an instance of fragment and map that to int
c

Chung

03/26/2020, 6:09 AM
That makes sense. Thanks for your explanation👍
d

Daniel

03/26/2020, 12:10 PM
nice