android-architecture-components

AndroidViewModel with SavedState

心不动则不痛 提交于 2020-04-16 02:47:23
问题 I need to use an AndroidViewModel with application context and a SavedStateHandle . I have it already working with application context, but I fail adding a SavedStateHandle to it. This is what I have, with only the application context: // A1. get ViewModel in Fragment val viewModel = ViewModelProvider(viewLifecycleOwner).get(MyViewModel::class.java) // A2. MyViewModel derives from my custom BaseAndroidViewModel class MyViewModel(application: Application) :BaseAndroidViewModel(application) //

Possible to access AndroidViewModel of Activity via Fragment?

痴心易碎 提交于 2020-04-14 07:45:23
问题 In the summer of last year I started refactoring my Android application with Android's architecture components (Room, ViewModel, LiveData). I have two Room repositories, one of them is accessed by multiple views (fragments) of the application. Because of that I used an AndroidViewModel , which has access to this repository and which is initialized in my MainActivity . new ViewModelProvider(this).get(CanteensViewModel.class); In my two fragments I accessed this ViewModel by new

How to mock Application class to unit test ViewModel

↘锁芯ラ 提交于 2020-04-11 05:50:31
问题 I have a View Model that extends AndroidViewModel class MoveViewModel(application: Application): AndroidViewModel(application),CoroutineScope{ .... } And I want to unit test it but I cannot figure out how to Mock the Application class @Test fun testSearchDataValidation() { val application = Mockito.mock(Application::class.java) val viewModel = MoveViewModel(application) ..... } But when I go to run the test I get an error that Mockito cannot mock Application org.mockito.exceptions.base

How to mock Application class to unit test ViewModel

最后都变了- 提交于 2020-04-11 05:50:06
问题 I have a View Model that extends AndroidViewModel class MoveViewModel(application: Application): AndroidViewModel(application),CoroutineScope{ .... } And I want to unit test it but I cannot figure out how to Mock the Application class @Test fun testSearchDataValidation() { val application = Mockito.mock(Application::class.java) val viewModel = MoveViewModel(application) ..... } But when I go to run the test I get an error that Mockito cannot mock Application org.mockito.exceptions.base

MotionLayout with vertical ScrollView

旧巷老猫 提交于 2020-04-10 14:01:45
问题 I'm trying to make animation with motion layout, and want to set scrollView as targetAnchor for motion scene onSwipe Transition, but it doesn't work. Maybe I don't know some thing about onSwipe transition. Here is my layout file <androidx.constraintlayout.motion.widget.MotionLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color

How to update UI with network error response in MVVM using NetworkBoundResource

穿精又带淫゛_ 提交于 2020-04-10 05:34:41
问题 I'm trying to implement MVVM pattern usingGoogle's android architecture components while using RX Java in NetworkBoundResource . However I'm having a difficult time finding a way to communicate the error response from network call to activity. here is a link to the original github project. I have read this post about "Refactoring google's NetworkBoundResource class to use RxJava instead of LiveData" but still not clear how to actually solve the problem. would appreciate if you could direct me

How to use a ViewModelProvider.Factory when extends from AndroidViewModel

99封情书 提交于 2020-04-06 02:34:19
问题 I want to send an extra parameter to my ViewModel, but this extends from AndroidViewModel . How can I add this parameter to the ViewModelFactory class ? ViewModel class ProjectViewModel(application: Application) : AndroidViewModel(application) { // need a param for project id... } ViewModelFactory class ProjectViewModelFactory(val projectId: Int): ViewModelProvider.Factory { override fun <T : ViewModel?> create(modelClass: Class<T>): T { // need to send this... return ProjectViewModel

still have error no current navigation node even after using navigation 2.2.1

痞子三分冷 提交于 2020-03-25 13:46:02
问题 I have tried to read this but it doesn't solve my issue so I navigate to next destination using this code val nextDirection = CreateEventNameFragmentDirections.actionToCreateEventType(userKM, newEvent) findNavController().navigate(nextDirection) but it crash after I rotate the device with error java.lang.IllegalStateException: no current navigation node i update the gradle using this implementation 'android.arch.navigation:navigation-fragment-ktx:2.2.1' implementation 'android.arch.navigation

still have error no current navigation node even after using navigation 2.2.1

回眸只為那壹抹淺笑 提交于 2020-03-25 13:45:33
问题 I have tried to read this but it doesn't solve my issue so I navigate to next destination using this code val nextDirection = CreateEventNameFragmentDirections.actionToCreateEventType(userKM, newEvent) findNavController().navigate(nextDirection) but it crash after I rotate the device with error java.lang.IllegalStateException: no current navigation node i update the gradle using this implementation 'android.arch.navigation:navigation-fragment-ktx:2.2.1' implementation 'android.arch.navigation

still have error no current navigation node even after using navigation 2.2.1

两盒软妹~` 提交于 2020-03-25 13:45:02
问题 I have tried to read this but it doesn't solve my issue so I navigate to next destination using this code val nextDirection = CreateEventNameFragmentDirections.actionToCreateEventType(userKM, newEvent) findNavController().navigate(nextDirection) but it crash after I rotate the device with error java.lang.IllegalStateException: no current navigation node i update the gradle using this implementation 'android.arch.navigation:navigation-fragment-ktx:2.2.1' implementation 'android.arch.navigation