android-viewmodel

Generated ViewDataBinding class in Android Studio 3.2.1 has an error when view layout data variable type is contained in a subpackage

◇◆丶佛笑我妖孽 提交于 2019-12-11 00:45:15
问题 I have my viewmodels contained in a "ViewModels" package. When setting one of them as a data variable type in my fragment layout xml file the generated ViewDataBinding class attempts to import the package as if it were a file. For example: import com.xyz.myapp.ViewModels; Rather than: import com.xyz.myapp.ViewModels.*; It then goes on to reference the viewmodel as ViewModels.MyFragmentViewModel causing further errors. I found that a workaround for this is to put all my viewmodel files in my

Is it ok to use sharedPrefrence with Coroutine kotlin

拟墨画扇 提交于 2019-12-10 23:09:25
问题 I have injected sharedPreference in ViewModel . Can I use android specific resource's while embedding Coroutine scope which automatically suspended when ViewModel loses scope. I mean is it ok to use preferende in ViewModel if we add a viewModel launch scope A CoroutineScope keeps track of all coroutines it creates. Therefore, if you cancel a scope, you cancel all coroutines it created @ContributesViewModel class SplashViewModel @Inject constructor(private val prefs: PrefStore) : BaseViewModel

Update viewmodel from a different class

北慕城南 提交于 2019-12-10 11:57:25
问题 I am trying to understand ViewModel and LiveData. In MainActivity, I am observing LiveData In MyTask, I am setting data on the LiveData, that should be displayed in the activity. Problem is data set in MyTask is not getting updated on the UI. MainActivity public class MainActivity extends AppCompatActivity { private MyViewModel viewModel; private TextView tv2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity

Am I allowed to observe a ViewModel, if I clean up the back references?

牧云@^-^@ 提交于 2019-12-10 10:19:26
问题 The suggested way to implement ViewModel is to expose the changing data by using LiveData objects to activities, fragments and views. There are cases, when LiveData is not an ideal answer or no answer at all. The natural alternative would be, to apply the observer pattern to the ViewModel , make it an observable. When registering observers to the ViewModel , the ViewModel will hold callback references to notify the observers. The documentation says, a ViewModel must not hold references to

Does ViewModel survive Activity save and restore?

放肆的年华 提交于 2019-12-10 00:58:57
问题 Instances of the new ViewModel class can survive configuration changes if used in the following fashion: mViewModel = ViewModelProviders.of(this).get(MyViewModel.class); However, in addition to configuration changes, there is also a save-restore scenario when the entire application's process is being killed. Will fields' values inside ViewModel be preserved during save-restore scenario? Edit: based on the answer to this question, I wrote this article: Android ViewModel Architecture Component

How to generate composed view models with temporary sub views?

笑着哭i 提交于 2019-12-09 21:39:41
问题 Scenario I have a quizz generator, which generates a sequence of quizzes of different classes. The sequence is of unlimited length. There is a view model for the quizz generator. There is a view model for each type of a quizz. The quizz generator view model should create the view models of the quizzes depending on their classes. Issue A view model must not hold a reference to the lifecycle, but I need the lifecycle to create view models. ViewModelProviders.of(lifecycle).get(classForQuizzType)

Shared ViewModel to help communication between fragments and parent activity

我的梦境 提交于 2019-12-09 06:29:20
问题 While Navigation component of JetPack looks pretty promising I got to a place where I could not find a way to implement something I wanted. Let's take a look at a sample app screen: The app has one main activity, a top toolbar, a bottom toolbar with fab attached. There are 2 challenges that I am facing and I want to make them the right way. 1. I need to implement fragment transactions in order to allow replacing the fragment on the screen, based on the user interaction. There are three ways I

ViewModelProviders is deprecated in 1.1.0

我只是一个虾纸丫 提交于 2019-12-06 16:35:07
问题 Looking at the Google docs for ViewModel , they show the below sample code on how to get a ViewModel : val model = ViewModelProviders.of(this).get(MyViewModel::class.java) When using the latest dependency android.arch.lifecycle:extensions:1.1.1 there is no such class ViewModelProviders . Going to the documentation for ViewModelProviders , I saw a comment saying: This class was deprecated in API level 1.1.0. Use ViewModelProvider.AndroidViewModelFactory The problem is, when trying to use

Accessing strings.xml from ViewModel

﹥>﹥吖頭↗ 提交于 2019-12-06 02:59:55
I am using Dagger 2 DataBindng and the new Android Lifecycle components, which have ViewModels . Inside my ViewModel how could I get access to my strings.xml? I was thinking at first, to inject a Context into the viewModel , however, this will just leak memory. Are there any other ways? There is an AndroidViewModel , which receives Application instance as parameter. From docs: Application context aware ViewModel . Subclasses must have a constructor which accepts Application as the only parameter. You can retrieve a string from strings.xml using that parameter. The repo in the link, however

Refresh data from a network using LiveData

£可爱£侵袭症+ 提交于 2019-12-05 12:43:23
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 is a network connection. The issue is when I click the button, two network calls are triggered, one