https://kotlinlang.org logo
Title
w

william

05/13/2019, 6:09 PM
do you guys think its better to send a bundle to each fragment (that requires identifiers about things they are showing) or to use some injected object that holds this state for each fragment, which the fragment can refer to
l

LeoColman

05/13/2019, 7:45 PM
I think an object might be cleared, right?
w

william

05/13/2019, 7:48 PM
what do you mean by cleared @LeoColman
im currently using the bundle approach but its just getting to be a lot with many fragments requiring the info
l

LeoColman

05/13/2019, 7:49 PM
I think objects might be garbage collected, for example, and you may lose your state over time
Perhaps you can wrap your bundle in an utility class?
p

pawegio

05/13/2019, 8:36 PM
sending bundles is redundant with view model shared by fragments, it’s better to hold such state in
LiveData
or
BehaviorRelay
l

LeoColman

05/13/2019, 8:37 PM
Perhaps they're not using mvvm
p

pawegio

05/13/2019, 8:54 PM
actually it’s applicable to mvp or mvi too, the idea is just to have an observable state, with or without either architecture components or RxJava
w

william

05/13/2019, 8:59 PM
i am using mvvm
and currently i have a unique view model per fragment. is the idea here to share a view model that holds this state?
p

pawegio

05/13/2019, 9:01 PM
exactly
it fits single-activity approach and works with navigation component
w

william

05/13/2019, 9:12 PM
interesting idea - i haven't thought about sharing view models this way before but i suppose it does make sense. thanks
j

Jan

05/14/2019, 6:43 AM
i aggree with pawel, sharing viewmodels is easier to use, particularly it's easier to inject a viewmodel rather than passing an argument over a couple of fragments that said you can make arguments easier to use, by using a readwritepropery, e.g. https://github.com/exozet/AndroidCore/blob/master/core/src/main/java/com/exozet/android/core/storage/FragmentArgumentDelegate.kt
p

Pieter

05/14/2019, 8:43 AM
I use MVVM pattern but not the architecture components. For flows within one activity that needs a state object I still pass the bundle up then assign in into the VM for the fragment which inherits from a BASE VM for the flow. Not sure if this helps
w

william

05/14/2019, 3:40 PM
in order for this to work, the vm would have to live in my god activity right?
p

pawegio

05/14/2019, 9:52 PM
yes,
ViewModel
needs to be bound to activity in case to be shared by fragments