r/android_devs • • Jun 14 '20

Help Getting Viewmodel into Custom/Compound view?

Do you guys have any suggestions? I'm using Navigation components which is what makes this tough. Do I just have an interface without worrying about Navigation components at all? Thanks so much.

11 Upvotes

5 comments sorted by

6

u/Zhuinden EpicPandaForce @ SO Jun 14 '20

It depends on what you want to do. Where there's a will, there's a way. You can get the Fragment via findFragment(), you can get the Activity through the base context chain, and you can get the NavController through the context too so you can even find the NavBackStackEntry.

But the simplest way to do it would be to have an interface in the custom view, implemented by the host fragment, the host fragment sets itself as the implementation of this interface and then the Fragment talks to the ViewModel.

2

u/UselessAccount45721 Jun 14 '20

Ended up using interfaces all over. You're right Zhuinden, I've been getting a little lazy these past few days :(

Feeling depressed since a few days and it's affecting my productivity. Will Google and read a bit more before posting questions from now. Thanks for your response, I knew you would answer.

4

u/anarcho-cummunist Jun 14 '20

Feeling depressed since a few days and it's affecting my productivity.

Take a break and do something else for 1-2 days. Self-care is important, and if you're learning a lot you need to give your brain time to relax!

3

u/anemomylos 🛡️ Jun 14 '20

Hi.

Using the "Help" flair makes it easier for others to locate a post asking for help.