android-livedata

is observeForever lifecycle aware?

こ雲淡風輕ζ 提交于 2019-12-30 10:01:21
问题 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

How to create LiveData which emits a single event and notifies only last subscribed observer?

北慕城南 提交于 2019-12-29 07:13:08
问题 I created live data which emits a single event as in this example. My question is next: How to notify only last subscribed observer when the value in the LiveData changes? What comes to my mind is to store observers in the linked list in SingleLiveData class and then to call super.observe only if a passed observer is the same as the last element of the list. I'm not sure if this is the best approach. I want to use this mechanism to propagate FAB click events from activity to the fragments

Error:Program type already present: android.arch.lifecycle.LiveData

為{幸葍}努か 提交于 2019-12-28 06:18:26
问题 When I press the run button in Android Studio, my app compiles but shows this error (redacted): Error:Program type already present: android.arch.lifecycle.LiveData (Full log) I've tried deleting the .gradle folder, then going to Build > Clean Project and Build > Rebuild Project . However, it doesn't work. I've also tried deleting the source code, then cloning again from git and importing the folder to Android Studio. However, it still produces that error. Here's my app's app/build.gradle :

Room - LiveData observer does not trigger when database is updated

怎甘沉沦 提交于 2019-12-28 04:12:06
问题 I am trying to find out in the code below, why is it that Room's LiveData observable does not give me new shifts once I populate the database with new data. This is put on my activity's onCreate method: shiftsViewModel = ViewModelProviders.of(this).get(ShiftsViewModel.class); shiftsViewModel .getShifts() .observe(this, this::populateAdapter); This is the populateAdapter method: private void populateAdapter(@NonNull final List<Shift> shifts){ recyclerView.setAdapter(new

Android custom dialog with MVVM

↘锁芯ラ 提交于 2019-12-25 18:57:22
问题 I am creating custom dialog and i want when the user click add button go and call retrofit and observe on changes but i don't know how to pass lifecycleowner to the observer private void observeViewModel(ProjectListViewModel viewModel) { // Update the list when the data changes viewModel.getProjectListObservable().observe( ***what to pass here ??*** , new Observer<List<Project>>() { @Override public void onChanged(@Nullable List<Project> projects) { if (projects != null) { //… projectAdapter

Will the `onChanged` method trigger just as we change the data? Even if we dont change the state of activity?

半腔热情 提交于 2019-12-25 00:54:41
问题 Will the onChanged method be called if we dont change the state of the activity or restart the activity? ( Even if the data changes ) . If it doesnt happen how shall i make it happen ? onCreate() { viewModel.getHolidaysDatesFromServer(..).observe(.....this,new Observer<GetHolidaysDatesFromServer>) { onChanged(GetHolidaysDatesFromServer GetHolidaysDatesFromServer) } } and my ViewModel class ... public class CalendarLifeCycleOwner extends AndroidViewModel { Context context; MutableLiveData

Observe one LiveData into other LiveData Observr

不打扰是莪最后的温柔 提交于 2019-12-24 21:42:14
问题 I have two LiveData : MutableLiveData<Int> -User could choose number by taping at "+" and "-" buttton. LiveData> - it's my data from RoomData base by call method getLessonsForThatDay(number:Int) . I have to update my method getLessonForThatDay(value) with that MutableLiveData value. I've tried to use MediatorLiveData<> but i don't get it. viewModel.dayOfWeek.observe(viewLifecycleOwner, androidx.lifecycle.Observer { dayOfWeekValue -> d("$dayOfWeekValue") viewModel.getLessonsForThatDay

FastAdapter flickers after loading from LiveData even though the data stays same

…衆ロ難τιáo~ 提交于 2019-12-24 16:17:59
问题 For the question I have prepared a simple and working example at Github: My example app downloads a JSON array containing top 30 players in a game using okhttp and stores them into SQLite Room. In the fragment I observe the corresponding LiveData<List<TopEntity>> object and update the FastAdapter instance: public class TopFragment extends Fragment { private final ItemAdapter<TopItem> mItemAdapter = new ItemAdapter<>(); private final FastAdapter<TopItem> mFastAdapter = FastAdapter.with

Combine two LiveData objects with the same observer types

て烟熏妆下的殇ゞ 提交于 2019-12-24 11:45:51
问题 I have two Room entities, both of which derive from the same custom base class. @Entity public class BaseEntity {} @Entity public class EntityA extends BaseEntity { ... } @Entity public class EntityB extends BaseEntity { ... } Both derived classes have a corresponding Dao interface. @Dao public interface BaseDao {} @Dao public interface DaoA extends BaseDao { @Query("SELECT * FROM EntityA") public LiveData<List<EntityA>> getAll(); } @Dao public interface DaoB extends BaseDao { @Query("SELECT

android paging library - which DataSource I should use

两盒软妹~` 提交于 2019-12-24 08:34:04
问题 What I Want: I want to implement Android Paging Library with network call only. What I Understood: So, DataSource has three classes that I can extend. PageKeyedDataSource: If your web service has pageNumber in url then you can use it. ItemKeyedDataSource: When you are fetching nested data then you can use it. For example getting replies on particular comment. PositionalDataSource: You can use when you know exact number of data is going to load. What I Tried : Code is below. MainActivity.java