Mar 2 | 7:20 PM |
Mark M. | has entered the room |
Mark M. | turned on guest access |
Mar 2 | 7:45 PM |
Erik | has entered the room |
Mark M. |
hello, Erik!
|
Mark M. |
how can I help you today?
|
Erik |
Mark, what's up?
|
Mark M. |
not much... and you?
|
Erik |
Can you recommend a section in any of your books giving examples of a shared viewmodel passing data between fragments please?
|
Mar 2 | 7:50 PM |
Mark M. |
I don't think I actually have one right now, from the looks of it
|
Mark M. |
I need to add something for that
|
Mark M. |
I will try to add something in *Elements of Android Jetpack* for the next update, though that's probably a couple of months out
|
Erik |
sure no prob. Do you have a section talking about advantages of a single activity architecture?
|
Mark M. |
not directly -- on the whole, I try to keep the architectural elements of my books light, just because recommendations keep changing there
|
Mark M. |
plus, in some cases, I need to artificially add activities, just to show how to start activities
|
Mark M. |
for example, in *Exploring Android*, there's no logical reason for the about screen to be its own activity... except to demonstrate starting an activity
|
Mar 2 | 7:55 PM |
Mark M. |
but, it's a good point -- right now, Google is pretty firm about minimizing your activity count, so I should cover that too
|
Erik |
I read an article saying many fragments and 1 activity is an advantage because you can share data across screens using a shared viewmodel. But you can use a shared viewmodel if you use multiple activities too
|
Mark M. |
"But you can use a shared viewmodel if you use multiple activities too" -- not between activities
|
Mark M. |
(at least, not by default -- I'm sure you could work up something if you really tried)
|
Erik |
ok can you explain?
|
Mark M. |
explain... what? :-)
|
Erik |
I've used a viewmodel for an activity before
|
Erik |
so I don't see why I can't use it again in a second one
|
Mark M. |
you can use the *class* again
|
Mark M. |
however, when you go to retrieve the *instance*, each activity has its own ViewModelProvider, and so each activity gets its own instance
|
Mar 2 | 8:00 PM |
Mark M. |
just so I can put in a useful code snippet... are you a Java person or a Kotlin person?
|
Erik |
I think you can specify a singleton viewmodel if you use a DI framework
|
Mark M. |
that would fall under "I'm sure you could work up something if you really tried"
|
Mark M. |
and I don't know how you would handle the lifecycles properly
|
Erik |
sure, kotlin is fine
|
Mark M. |
basically, when you do something like private val vm: EventViewModel by viewModels(), that viewModels() property delegate is going to work with a ViewModelProvider that is unique to your activity
|
Mark M. |
you can provide an alternative factory via a lambda (by viewModels { ... })
|
Mark M. |
and through that, you could hack something together that could arrange to share a viewmodel between activities
|
Mark M. |
but, I don't know how you will determine when to clear the viewmodel
|
Erik |
ok, can you please compare it to its fragment alternative, using 1 viewmodel instead
|
Mark M. |
in a fragment, by viewModels() will use a ViewModelProvider scoped to the fragment
|
Mark M. |
but by activityViewModels() will use a ViewModelProvider scoped to the activity
|
Mark M. |
so if two fragments in the same activity both use by activityViewModels() for the same ViewModel subclass, they both get the same instance
|
Mark M. |
and, if the activity uses by viewModels() for that same ViewModel subclass, it would get the same instance that the fragments do
|
Mar 2 | 8:05 PM |
Mark M. |
that viewmodel will be cleared when the activity is destroyed
|
Erik |
ok that makes sense now. I appreciate your explanation mark
|
Erik |
take care
|
Mark M. |
you too!
|
Mark M. |
and thanks for the suggestions of topics to cover!
|
Erik |
looking forward to reading about it in future editions
|
Mar 2 | 8:15 PM |
Erik | has left the room |
Mar 2 | 8:30 PM |
Mark M. | turned off guest access |