Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to create viewmodel instance for a lot of fragments?

Posted on November 11, 2022 By

Solution 1 :

You can use sharedViewModel and it will be shared across all fragments in activity https://developer.android.com/topic/libraries/architecture/viewmodel#sharing

Problem :

I have a lot of fragments in my activity, each of which uses own methods in the VM. In each of fragment I have to create viewmodel instance before using it in such way:

val retrofitService = RetrofitService.getInstance(requireContext())
val mainRepository = MainRepository(retrofitService)
val viewVM = ViewModelProvider(
    [email protected],
    AppVMFactory(mainRepository)
).get(AppViewModel::class.java)

in my MainRepository I have my requests methods:

class MainRepository constructor(private val retrofitService: RetrofitService) {
    suspend fun someMethod() = retrofitService.someMethod()
}

and in retrofit service I have api requests methods. The main question is how I cane create viewModel instance only one time without using this code:

val retrofitService = RetrofitService.getInstance(requireContext())
val mainRepository = MainRepository(retrofitService)
val viewVM = ViewModelProvider(
    [email protected],
    AppVMFactory(mainRepository)
).get(AppViewModel::class.java)

in each fragment. At the beginning I thought about single fragment parent, but I’m not sure whether it will be ok for dialogFragment and also how it will be able to use with data binding. Maybe I can use some features of android development about which I don’t know?

Comments

Comment posted by developer.android.com/training/dependency-injection/…

Hi, you can try using Hilt for this. There will be much less code for the same. Go to this link for how to use Hilt.

READ  [FIXED] java - How to Save the last Fragment?
Powered by Inline Related Posts
Android Tags:android, android-fragments, android-viewmodel

Post navigation

Previous Post: [FIXED] android.media.ImageWriter produces green screen screen on every second posted Image frame
Next Post: [FIXED] android – Notifee native module not found

Related Posts

[FIXED] java – how to design Linear layout programmatically Android
[FIXED] java – ‘android.annotation.NonNull’ is not public in ‘android.annotation’. Cannot be accessed from outside package Android
[FIXED] android – TextView text and its drawable to the center of textview Android
[FIXED] java – Passing Context From Service Class to another Service Class Android
[FIXED] c# – Xamarin Android: Resource android: style/AppTheme.Base Not Found Android
[FIXED] sqlite – How/Where do I store my .db file on android Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Which is stronger 19 gauge or 23 gauge?
  • Does BMW still use GM transmissions?
  • Is primary or secondary market research better?
  • What does it mean when ADT says low battery?
  • Can a baby sleep on a regular mattress?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme