Looking here: <https://developer.android.com/guide...
# android
e
Looking here: https://developer.android.com/guide/fragments/communicate
Copy code
class ListFragment : Fragment() {
    // Using the activityViewModels() Kotlin property delegate from the
    // fragment-ktx artifact to retrieve the ViewModel in the activity scope
    private val viewModel: ItemViewModel by activityViewModels()
wtf is
activityViewModels
?
đŸ˜¶ 2
a
Returns a property delegate to access parent activity’s ViewModel, if
factoryProducer
is specified then
ViewModelProvider.Factory
returned by it will be used to create ViewModel first time. Otherwise, the activity’s
androidx.activity.ComponentActivity.getDefaultViewModelProviderFactory
will be used.
so,
activityViewModels()
is a delegation used to create or access an instance of ViewModel by the lazy way
in this case, the Activity’s ViewModel was instantiated here:
Screen Shot 2022-01-26 at 18.02.47.png
e
@Arilson JosĂ© de Oliveira JĂșnior Thanks! I think I get it.
👍 1
a
Good! It’s common instantiate ViewModel with
viewModel()
or
sharedViewModel()
delegate. Read something about that.