Discover more from MB_Coder Newsletter
Is it a good idea to have different View models for different Navigation fragments?
ViewModel and Navigation Fragment
You should have a ViewModel associated with a Fragment that's just easier for you to separate the logic so that ViewModel only handles the logic for that Fragment.
The best thing about View models is that they're a conduit for information.
Say you've got your repository layer, your databases, and you don't want your screen to have a hard dependency on repositories and ViewModel kind of that transform layer where you're taking things from your repository and then making them into the exact thing that your UI needs so say you don't actually need all 30 fields of your object and that ViewModel can kind of be that translation layer because you only need the name and the bitmap of the user's profile and ViewModel bring that data through and having it specific to that screen makes so much easier to kind of be able to test out each layer separately from the others.