android-viewmodel

Android Architecture Component ViewModel - How to mock ViewModel on test Activity?

删除回忆录丶 提交于 2019-12-23 07:42:26
问题 I'm trying to setup UI testing similar to GithubBrowserSample and it looks like the sample project only has mock ViewModel for Fragment but not an example for Activity . Here's my code where I am trying to test the Activity by mocking ViewModel . But the ViewModel is not getting set before onCreate() in Activity. @RunWith(AndroidJUnit4::class) class MainActivityTest { val viewModel = mock(MainViewModel::class.java) @Rule @JvmField val activityRule = ActivityTestRule<MainActivity>(MainActivity

Accessing strings.xml from ViewModel

心已入冬 提交于 2019-12-22 10:41:32
问题 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? 回答1: 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

Android Architecture Components: ViewModel/Repository vs bind to Service/IntentService

北战南征 提交于 2019-12-22 06:41:05
问题 I want to implement/refactor an app to the Android Architecture Components concept, see https://developer.android.com/jetpack/docs/guide In this topic Android Architecture Components ViewModel - communication with Service/IntentService, I found a very good answer to the architectural issue, see https://stackoverflow.com/a/46736146/6251815 But i want to ask, how to bind the service from a repository, because we have neither context nor activity here. To be clear on that, the question is about

Kotlin does not understand ViewModelProviders.of(activity ?: fragment)

て烟熏妆下的殇ゞ 提交于 2019-12-21 17:09:55
问题 Inside my Fragment I initialize a ViewModel using ViewModelProviders . I want it to take its Activity if not null , otherwise itself ( Fragment ). private val viewModel: MainViewModel by lazy { ViewModelProviders.of(activity ?: this).get(MainViewModel::class.java) } None of the following functions can be called with the arguments supplied. of(Fragment) defined in androidx.lifecycle.ViewModelProviders of(FragmentActivity) defined in androidx.lifecycle.ViewModelPro It seems the language does

viewmodel share data between activity

ⅰ亾dé卋堺 提交于 2019-12-21 12:59:38
问题 I'm using android viewmodel, but I'm not able to pass and access data of an activity viewmodel from another activity. I can only do that with fragment. Should I use library like EventBus to share data between few activity? What is the best pratice? 来源: https://stackoverflow.com/questions/52315691/viewmodel-share-data-between-activity

LiveData not Observing after first Call

北城以北 提交于 2019-12-20 06:18:12
问题 I implemented LiveData & ViewModel to mimic AsyncTaskLoader. I load file names from the camera directory in DCIM, and then i attach a fileObserver to Observe when a File (picture) is deleted, and then a callback tells the LiveData to re-fetch the fileNames when delete event occurs The Problem: The code below should fetch the file Names from DCIM/Pictures asynchronously with the help of LiveData and then a FileObserver is attached to the directory (DCIM/Pictures), to monitor when a file is

View Model keeps creating instance of Live Data

怎甘沉沦 提交于 2019-12-19 19:17:06
问题 I created the instance of View Model in onCreate method of an activity. ticketViewModel = ViewModelProviders.of(this).get(TicketViewModel.class); Then i have a method, AddTicket , which uses viewModel to hit a service and on response from viewModel i dismiss loading animation. public void addTicket(View view){ ticketViewModel.AddTicket(id).observe(this, response ->{ dismissLoadingAnimation(); } Now after adding a ticket, user can repress the Add Ticket button, and the addTicket() method will

How does Android Paging Library know to load more data?

走远了吗. 提交于 2019-12-18 20:29:11
问题 Iʼm fairly new to developing Android apps and Iʼm trying to do everything “the right way.” So right now, Iʼm implementing the new Android Paging Library into my project, where I need to load a list of articles from a network server. I have an ArticlesRepository class that returns an ArticleList class containing instances of ArticleListItem that I would like to display in a RecyclerView. The list of articles is paginated already on the server, so the repository sends a request for the first

How to share an instance of LiveData in android app?

南楼画角 提交于 2019-12-17 21:16:34
问题 Simple use case I am using MVVM architecture and Android Architecture Components in my app. After user logs in, I am fetching multiple network data and want to have access to it from different ViewModels attached to different Activities lifecycle. I don't want to use Room Persistence Library in my app. I have seen some question about sharing a ViewModel between Activities or using a LiveData as static member, but I think most of the cases we need to access the same data in multiple screens. I

LiveData update on object field change

扶醉桌前 提交于 2019-12-17 15:24:53
问题 I'm using Android MVVM architecture with LiveData. I have an object like this public class User { private String firstName; private String lastName; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } } And my view model looks like this public class InfoViewModel extends AndroidViewModel {