question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there a way to have a sharedViewModel for a Fragment's Scope?

See original GitHub issue

I’m using Android Navigation Component, in a Single-Activity App, there is a ViewModel that I want to be re-created and it’s shared between some DialogFragments that compose this specific flow. I want to know if there is a way to have a sharedViewModel tied to this FragmentScope

I would like to use a Scoped sharedViewModel, I think that this will improve the library capacities

As an alternative, I would like to know if there is a way to clear a viewModel instance when the fragment is destroyed

koin-android

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:34 (4 by maintainers)

github_iconTop GitHub Comments

41reactions
krasavello13commented, Feb 24, 2020

Hi, currently I’m using: Koin 2.1.0-beta-1 and this version of library have more elegant way to do it.

So, try to do like this :

private val viewModel by lazy { 
     requireParentFragment().getViewModel<MyViewModel>()
} 

To more deep understanding how it works, please check the package org.koin.androidx.viewmodel.ext.android of Koin library

17reactions
krasavello13commented, Oct 4, 2019

Yes, you can set the scope like this: sharedViewModel<ViewModel>(from = {//your scope})

In my case I have ViewPager, and i use the same viewModel for internal Fragments like: val viewModel by sharedViewModel<MyViewModel>(from = { parentFragment!! })

Read more comments on GitHub >

github_iconTop Results From Across the Web

Shared ViewModel Across Fragments
You will learn how to use a shared ViewModel to share data between the fragments of the same activity and new concepts like...
Read more >
Sharing data between fragments using new architecture ...
These fragments can share a ViewModel using their activity scope to handle this communication. And shows a implementation example: public class ...
Read more >
Shared ViewModel in Android
In android, we can use ViewModel to share data between various fragments or activities by sharing the same ViewModel among all the fragments...
Read more >
Communicate Between Fragments Using ViewModel
Fragments can't and shouldn't talk to each other. Communication between fragments should be done via the associated activity . Fragments shouldn't have any...
Read more >
Shared ViewModel - Explained | Android Studio Tutorial
Which make sense. In order to Share values from the same ViewModel instance between multiple Fragments, you need to scope ViewModel to an ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found