need little bit clarity on one thing, right now working on a flow where I've two view model and one is dependent on other , to handle that thing I tried injecting both view models in my Base Activity the problem I faced, with this approach. Every time when I try to access those view model, Base Activity was returning me new instances, is there any way I can manage scope of that view model so I can easily access its properties without adding extra Api calls.
Module Class
val homeModule= module {
single { HomeDispatcher(get(), get()) }
viewModel { HomeViewModel (get()) }
}
**Main Activity**
import org.koin.android.viewmodel.ext.android.viewModel
class NotificationActivity : BaseActivity() {
private val viewModel: HomeViewModel by inject()
private val viewModel: HomeViewModel by viewModel()
}
class NotificationsFragment : BaseFragment() {
private val viewModel: HomeViewModel by viewModels({requireActivity()})
private val viewModelx: HomeViewModel by activityViewModels
}
> by using viewModels({requireActivity()}) getting instance not found.
Update for Hilt
implementation "androidx.activity:activity-ktx:1.2.0"
private val viewModelx: HomeViewModel by viewModels
the above-mentioned library has view models for injecting view models directly lazily.
import org.koin.android.viewmodel.ext.android.viewModel
import org.koin.android.viewmodel.ext.android.sharedViewModel
private val viewModel: HomeViewModel by viewModel()
so solved this issue by using viewModel()
in activity and sharedViewModel<MyViewModel>()
in fragment class