android-architecture-components

LiveData.getValue() returns null with Room

橙三吉。 提交于 2020-01-09 07:13:10
问题 Java POJO Object public class Section { @ColumnInfo(name="section_id") public int mSectionId; @ColumnInfo(name="section_name") public String mSectionName; public int getSectionId() { return mSectionId; } public void setSectionId(int mSectionId) { this.mSectionId = mSectionId; } public String getSectionName() { return mSectionName; } public void setSectionName(String mSectionName) { this.mSectionName = mSectionName; } } My Query method @Query("SELECT * FROM section") LiveData<List<Section>>

Trigger update for a LiveData member when another LiveData is updated in the view model

南楼画角 提交于 2020-01-06 02:29:30
问题 In a word game app I share a model between an activity and a fragment: public class MainViewModel extends AndroidViewModel { private LiveData<List<Game>> mGames; private final MutableLiveData<Game> mDisplayedGame = new MutableLiveData<>(); (please excuse the non-english text in the screenshot) The activity observes mGames being currently played by the user and updates the navigational drawer menu (see the left side of the above screenshot). The fragment observes mDisplayedGame and displays it

Fragment savedInstanceState is always null when using Navigation Component

感情迁移 提交于 2020-01-03 17:13:09
问题 Currently, I am playing around Android Navigation Component with Bottom Navigation Bar. While playing I realized two facts: Fragments are always recreated ( onCreate , onViewCreated , onViewDestroyed are called as soon as the user navigates to another fragment) savedInstanceState is always null (in onCreate , onViewCreated , etc.) The first issue can be fixed by using custom FragmentNavigator , which will reuse fragment if it already exists package am.chamich.apps.advancedbottomnavigation

Fragment savedInstanceState is always null when using Navigation Component

时间秒杀一切 提交于 2020-01-03 17:12:43
问题 Currently, I am playing around Android Navigation Component with Bottom Navigation Bar. While playing I realized two facts: Fragments are always recreated ( onCreate , onViewCreated , onViewDestroyed are called as soon as the user navigates to another fragment) savedInstanceState is always null (in onCreate , onViewCreated , etc.) The first issue can be fixed by using custom FragmentNavigator , which will reuse fragment if it already exists package am.chamich.apps.advancedbottomnavigation

Cannot create an instance of ViewModel class (Unable to start activity ComponentInfo)

核能气质少年 提交于 2020-01-03 09:24:19
问题 I am using MVVM, Retrofit, LiveData in my project but I get this error before that I saw these links Cannot create an instance of custom ViewModel Cannot create an instance of class ViewModel Error java.lang.RuntimeException: Unable to start activity ComponentInfo{ir.orangehat.movieinfo/ir.orangehat.movieinfo.application.home.HomeActivity}: java.lang.RuntimeException: Cannot create an instance of class ir.orangehat.movieinfo.application.home.HomeViewModel This is my ViewModel and I think the

Livedata returned by Room won't be triggered after GC

試著忘記壹切 提交于 2020-01-02 19:20:10
问题 Our team are building a project with benefits from Android Jetpack. There are demo code showing the question we are facing. These code can be found at https://github.com/viseator/TestRoomLivedata I create a UserDao : @Dao interface UserDao { @Query("SELECT * FROM user WHERE uid = :uid LIMIT 1") fun findUserById(uid: String?): Single<User> @Query("SELECT * FROM user WHERE state = 1 LIMIT 1") fun findLoginUserWithObserve(): LiveData<User> @Query("SELECT * FROM user WHERE state =1 LIMIT 1") fun

PagedListAdapter jumps to beginning of the list on receiving new PagedList

﹥>﹥吖頭↗ 提交于 2020-01-01 03:02:27
问题 I'm using Paging Library to load data from network using ItemKeyedDataSource . After fetching items user can edit them, this updates are done inside in Memory cache (no database like Room is used). Now since the PagedList itself cannot be updated (discussed here) I have to recreate PagedList and pass it to the PagedListAdapter . The update itself is no problem but after updating the recyclerView with the new PagedList , the list jumps to the beginning of the list destroying previous scroll

What is the difference between map() and switchMap() methods?

…衆ロ難τιáo~ 提交于 2019-12-31 17:38:13
问题 What is the difference between those 2 methods of the LiveData class? The official doc and tutorial are pretty vague on that. In the map() method the first parameter called source but in the switchMap() it called trigger . What's the rationale behind that? 回答1: As per the documentation Transformations.map() Applies a function on the value stored in the LiveData object, and propagates the result downstream. Transformations.switchMap() Similar to map, applies a function to the value stored in

BoundService + LiveData + ViewModel best practice in new Android recommended architecture

亡梦爱人 提交于 2019-12-31 08:43:17
问题 I been struggling a lot thinking about where to place Android Services in the new Android recommended Architecture. I came up with many possible solutions, but I cannot make up my mind about which one is the best approach. I did a lot of research, and I couldn't find any useful guideline nor tutorial. The only hint I found about where to place the Service in my app architecture is this one, from @JoseAlcerreca Medium post Ideally, ViewModels shouldn’t know anything about Android. This

is observeForever lifecycle aware?

不羁岁月 提交于 2019-12-30 10:01:30
问题 I'm working with MVVM, and I have made different implementations of it, but one thing that is still making me doubt is how do I get data from a Repository (Firebase) from my ViewModel without attaching any lifecycle to the ViewModel. I have implemented observeForever() from the ViewModel, but I don't think that is a good idea because I think I should communicate from my repository to my ViewModel either with a callback or a Transformation. I leave here an example where I fetch a device from