1

I am trying to use the same object in two ViewModels, but the object is being recreated each time.

Question. Am I doing something wrong, or is it supposed to happen?

⬇️⬇️⬇️ Here are my Classes. please look into it. Thanks for helping..

AppModule.kt

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
    @Provides
    fun provideModsLoader(): ModsLoader {
        return ModsLoader()
    }
}

FirstViewModel.kt

@HiltViewModel
class FirstViewModel @Inject constructor(
     modsLoader: ModsLoader
) : ViewModel() {
     ...
     here I adding value to my ModsLoader object's mutableList
}

SecondViewModel.kt

@HiltViewModel
class SecondViewModel @Inject constructor(
    modsLoader: ModsLoader,
    savedStateHandle: SavedStateHandle,
) : ViewModel() {
    ...
    here I am using that property modified in the last view model
    *** But I am getting empty list ***
}

ModLoader.kt

class ModsLoader {
    val modsByCategories = mutableListOf<ModsByCategory>()
    ...
}
sanket kheni
  • 1,482
  • 2
  • 12
  • 29

0 Answers0