Replies: 2 comments
-
I was looking for the same thing.
|
Beta Was this translation helpful? Give feedback.
0 replies
-
val fragmentsModule = module {
scope(named("fragments_shared")) {
scoped { SharedViewModel(get()) }
scoped { pository() }
}
}
class FirstFragment : Fragment(), KoinScopeComponent {
override val scope: Scope by lazy {
getKoin().getOrCreateScope("fragments_shared_scope", named("fragments_shared"))
}
private val sharedViewModel: SharedViewModel by scope.inject()
override fun onDestroy() {
super.onDestroy()
if (!requireActivity().supportFragmentManager.fragments.any {
it is SecondFragment || it is ThirdFragment
}) {
scope.close()
}
}
}
class SecondFragment : Fragment(), KoinScopeComponent {
override val scope: Scope by lazy {
getKoin().getOrCreateScope("fragments_shared_scope", named("fragments_shared"))
}
private val sharedViewModel: SharedViewModel by scope.inject()
}
class ThirdFragment : Fragment(), KoinScopeComponent {
override val scope: Scope by lazy {
getKoin().getOrCreateScope("fragments_shared_scope", named("fragments_shared"))
}
private val sharedViewModel: SharedViewModel by scope.inject()
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello.
I need to have a viewModel, that will be created with thecreation of parent fragment, and shared between it's child fragments, but not bound to the activity, and I can't seem to find a way to do so? Is it possibile with koin?
The idea is, I have a big form fragment, which is splited into few different fragments depending on some logic, and I need all of the child fragments to communicate with the parent's viewModel, however I've noticed that the viewModel stays alive after the parent fragment was killed, which causes issues while opening the parent fragment again! ( it loads it's state ).
Any idea how this could be achieved, koin or not?
Beta Was this translation helpful? Give feedback.
All reactions