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.

Get scoped viewmodel from child fragment

See original GitHub issue

Let’s say I have a module for my fragment.

    scope(named<MyFragment>()) {
        viewModel {
            MyFragmentVM(
                get(),
                get(),
                get(),
                get()
            )
        }
    }

From MyFragmentVM I open a dialog fragment, where I pass MyFragment’s scopeId. From my dialog on some action I would like to call my MyFragmentVM's method. Is there any way to get the MyFragmentVM? I can see some methods, but they require ViewModelParameters and so on. I need smth that does not require anything else but scope/scopeId.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:3
  • Comments:10

github_iconTop GitHub Comments

6reactions
TooLazyycommented, Dec 16, 2019

So here we’re back again. As I told before - I don’t need a sharedviewmodel for the case described above. I need to get my fragment’s NOT shared view model from a dialog. In case of shared vm it will outlive fragment, what’s not good in my case. So as I can see there’s no way to get NOT shared vm from a child fragment event if you have the scopeId.

1reaction
Kharloscommented, Jul 5, 2022

Yo can use this too

scope(named<YourFragmentClass>()) { viewModel { YourFragmentClassViewModel(get()) } scoped { UseCaseExample(get()) } }

and in your YourFragmentClass just add extended with AndroidScopeComponent and create

override val scope : Scope by fragmentScope()

and that’s all

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to scope a view model to a parent fragment?
Using Fragment-ktx libr in your app you can get viewModel as below in your app-> build.gradle
Read more >
Get parent fragment viewModel from child fragment inside ...
I have a parent fragment with ViewModel and viewPager2 inside it creates 4 child fragments, I want to get parent viewModel from child...
Read more >
Communicating with fragments - Android Developers
These fragments can share a ViewModel using their activity scope to handle this communication. By sharing the ViewModel in this way, the ...
Read more >
ViewModel scopes - RainbowCake
ViewModels by default are scoped to their Fragment, meaning a new instance is created for every new instance of the Fragment (barring configuration...
Read more >
A step by step guide on how to use Nav-graph Scoped ...
Scope your viewmodel to an Activity and move on with your life. ... Create a nested navigation graph that contains all fragments you'd...
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