android-viewmodel

injecting viewmodel with navigation-graph scope: NavController is not available before onCreate()

此生再无相见时 提交于 2020-02-15 00:21:08
问题 I'm using a navigation-component in my application and also using shared ViewModel between multiple fragments that are in the same graph. Now I want to instantiate the ViewModel with this graph scope with this. As you know, in fragments we should inject objects ( ViewModel,..etc ) in onAttach: but when I want to do this (injecting ViewModel with a graph scope in onAttach ), this error occurs: IllegalStateException: NavController is not available before onCreate() Do you how can I do this? 回答1

injecting viewmodel with navigation-graph scope: NavController is not available before onCreate()

老子叫甜甜 提交于 2020-02-15 00:20:10
问题 I'm using a navigation-component in my application and also using shared ViewModel between multiple fragments that are in the same graph. Now I want to instantiate the ViewModel with this graph scope with this. As you know, in fragments we should inject objects ( ViewModel,..etc ) in onAttach: but when I want to do this (injecting ViewModel with a graph scope in onAttach ), this error occurs: IllegalStateException: NavController is not available before onCreate() Do you how can I do this? 回答1

How to manually clear a specific viewmodel?

和自甴很熟 提交于 2020-02-02 06:47:26
问题 As I understand when we call this code in fragments, we will have a viewmodel instance from activity ViewModelStore: viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java) When fragments will end their lifecycle, the instance of this viewmodel will exist in ViewModelStore until the destruction of activity. ViewModelStore has a clear() method, but it clears all viewmodels in it. Is there some way to clear specific ViewModel? Another solution is to scope ViewModel to

How to manually clear a specific viewmodel?

时光怂恿深爱的人放手 提交于 2020-02-02 06:46:07
问题 As I understand when we call this code in fragments, we will have a viewmodel instance from activity ViewModelStore: viewModel = ViewModelProviders.of(activity!!).get(SharedViewModel::class.java) When fragments will end their lifecycle, the instance of this viewmodel will exist in ViewModelStore until the destruction of activity. ViewModelStore has a clear() method, but it clears all viewmodels in it. Is there some way to clear specific ViewModel? Another solution is to scope ViewModel to

Searching a LiveData of PagedList in RecyclerView by Observing ViewModel

人盡茶涼 提交于 2020-01-24 03:40:06
问题 With android Paging library it is really easy to load data from Database in chunks and ViewModel provides automatic UI update and data survival. All these frameworks modules help us create a great app in android platform. A typical android app has to show a list of items and allows user to search that list. And this what I want to achieve with my app. So I have done an implementation by reading many documentations, tutorials and even stackoverflow answers. But I am not so sure whether I am

The correct way to obtain a ViewModel instance outside of an Activity or a Fragment

这一生的挚爱 提交于 2020-01-22 19:26:21
问题 I'm building a location app where I display background locations from a Room database in my MainActivity. I can get a ViewModel by calling locationViewModel = ViewModelProviders.of(this).get(LocationViewModel.class); locationViewModel.getLocations().observe(this, this); Periodic background locations should be saved to the Room database when I receive location updates via a BroadCastReceiver. They should be saved by calling locationViewModel.getLocations().setValue() public class

The correct way to obtain a ViewModel instance outside of an Activity or a Fragment

折月煮酒 提交于 2020-01-22 19:26:02
问题 I'm building a location app where I display background locations from a Room database in my MainActivity. I can get a ViewModel by calling locationViewModel = ViewModelProviders.of(this).get(LocationViewModel.class); locationViewModel.getLocations().observe(this, this); Periodic background locations should be saved to the Room database when I receive location updates via a BroadCastReceiver. They should be saved by calling locationViewModel.getLocations().setValue() public class

Refresh data from a network using LiveData

纵饮孤独 提交于 2020-01-13 10:13:53
问题 I am working on an app that queries the github api to get a list of user and i'm following the recommended android architecture component guide. Once the data is fetched from the network, I store it locally using Room DB and then display it on the UI using ViewModel that observes on the LiveData object (this works fine). However, I want to be able to have a button which when clicked would trigger a refresh action and perform a network request to get new data from the API if and only if there

Refresh data from a network using LiveData

試著忘記壹切 提交于 2020-01-13 10:12:07
问题 I am working on an app that queries the github api to get a list of user and i'm following the recommended android architecture component guide. Once the data is fetched from the network, I store it locally using Room DB and then display it on the UI using ViewModel that observes on the LiveData object (this works fine). However, I want to be able to have a button which when clicked would trigger a refresh action and perform a network request to get new data from the API if and only if there

Scoping a viewmodel to multiple fragments (not activity) using the navigation component

自古美人都是妖i 提交于 2020-01-09 11:15:50
问题 I'm using the navigation component, I want a view model to be shared between a few fragments but they should be cleared when I leave the fragments (hence not scoping them to the activity) I'm trying to take the one activity many fragments approach. I have managed to achieve this using multiple nav hosts and scoping the fragments to it using getParentFragment but this just leads to more issues having to wrap fragments in other parent fragments, losing the back button working seamlessly and