I have 12 fragments UpcomingGamesFragment repeated twelve times for each month of the year, the respective fragment shows the game releases of the month. For example, the first month will show the games releasing in January 2019, the next fragment will have February 2019 releases, etc.
What I'm trying to build is an architecture that uses a ViewModel. A ViewModel which will be shared across all my 12 fragments and would trigger the data change (through LiveData) to all fragments gracefully, but I have no idea how to use this ViewModel class to accomplish the update to all visible fragments, here's my UpcomingGamesFragment class with the request month data method:
public class UpcomingGamesFragment extends Fragment {
public void loadReleaseData(final int refresh) {
if (mDatabaseLoading == null) {
Log.d(TAG, "Fragment filter " + mFilter + " [fragment is null]");
return;
} else {
Log.d(TAG, "Updating fragment: " + mFilter);
}
if (AppUtil.doesInternetWork(getActivity())) {
// update, data fetched from firebase
}
}
The 12 fragments are initialized in another fragment which gets shown in MainActivity, the fragment is called UpcomingViewPagerFragment which creates 12 UpcomingGamesFragment in a loop.
And here's my ViewModel class:
public class ReleasesViewModel extends ViewModel {
private MutableLiveData<List<_Release>> upcomingFragmentLiveData =
new MutableLiveData<>();
public ReleasesViewModel() {
}
public LiveData<List<_Release>> getUpcomingFragmentList() {
return upcomingFragmentLiveData;
}
}
So how can I update the 12 fragments with the loadReleaseData method taking in count the lifecycle of each?
Assuming all fragment contents are values held somewhere else in your code and assuming you are correctly updating those values. The easiest way to update all fragments accordingly is by calling:
notifyDataSetChanged();
You need to retrieve your ViewModel class object in fragment and register LiveData from it like,
ReleasesViewModel obj = ViewModelProviders.of(getActivity()).get(ReleasesViewModel.class);
in your fragment's onCreate() method, then use that obj of view model for observing your live data like,
(obj of your view model).getUpcomingFragmentList().observe(this, `your observer for that fragment`);
For your instance:
This will get called once any data change happen for your live data inside ViewModel
obj.getUpcomingFragmentList().observe(this, new Observer<List<_Release>>() >{
#Override
public void onChanged(#Nullable List<_Release> list) {
}
});
If you can't find ViewModelProviders class then you need to add dependecies for your ViewModel & livedata from here,
link to add view model & live data to your project
Related
I have a simple setup of 2 fragments: ConversationFragment and DetailsFragment
I am using Room with Paging 3 library and to populate the ConversationFragment I am using a PagingLiveData implementation together with a AndroidViewModel belonging to the ConversationFragment.
I am not using the Navigation Components here, just a common fragment navigation as per Android documentation.
From that fragment I can open the DetailsFragment and then return back to the fragment again. Everything is working well, until I open said fragment and return, then the observer that was tied in the ConversationFragment is lost since that fragment is being destroyed when opening the DetailsFragment.
So far this is not a big issue, I can restart the observer again and it does work when I do that.
However, when I attach the observer again the entire list reflows, this causes the items in the RecyclerView to go wild, the position the list was on is lost and the scrollbar changes sizes which confirms pages are being loaded/reloaded.
I could withstand the weird behavior to a degree, but to have the position lost on top of that is not acceptable.
I looked into caching the results in the view model, but the examples I could find in the available documentation are basic and do not show how the same could be achieved using a LiveData<PagingData<...> object.
Currently this is what I have:
ConversationFragment
#Override
public void onViewCreated(
#NonNull View view,
#Nullable Bundle savedInstanceState
) {
if (viewModel == null) {
viewModel = new ViewModelProvider(this).get(ConversationViewModel.class);
}
if (savedInstanceState == null) {
// adapter is initialized in onCreateView
viewModel
.getList(getViewLifecycleOwner())
.observe(getViewLifecycleOwner(), pagingData -> adapter.submitData(lifecycleOwner.getLifecycle(), pagingData));
}
super.onViewCreated(view, savedInstanceState);
}
ConversationViewModel
public class ConversationViewModel extends AndroidViewModel {
final PagingConfig pagingConfig = new PagingConfig(10, 10, false, 20);
private final Repository repository;
private final MutableLiveData<PagingData<ItemView>> messageList;
public ConversationFragmentVM(#NonNull Application application) {
super(application);
messageList = new MutableLiveData<>();
repository = new Repository(application);
}
public LiveData<PagingData<ItemView>> getList(#NonNull LifecycleOwner lifecycleOwner) {
// at first I tried only setting the value if it was null
// but since the observer is lost on destroy and the value
// is not it would never be able to "restart" the observer
// again
// if (messageList.getValue() == null) {
PagingLiveData.cachedIn(
PagingLiveData.getLiveData(new Pager<>(pagingConfig, () -> repository.getMessageList())),
lifecycleOwner.getLifecycle()
).observe(lifecycleOwner, messageList::setValue);
// }
return messageList;
}
}
As it is, even if I return the result of the PagingLiveData.cachedIn the behavior is the same when I return to the fragment; the items show an erratic behavior in the recyclerview list and the position it was on is totally lost.
This is what I was trying to achieve to see if it fixed my issue:
This is a code lab available here: https://developer.android.com/codelabs/android-training-livedata-viewmodel#8
As you can see the mAllWords are cached and they are only initialized when the view model is constructed for the first time, any subsequent changes are simply updates and would only require new observers to be attached when the fragment is destroyed and created again while still in the back stack.
This is what I was trying to do, but it does not work the way I thought it did, at least it is not as straight forward as I thought.
How can this be achieved?
There's quite a lot to unpack here but my best guess would be your getList method in ConversationViewModel. You're on the right track with using ViewModels and LiveData to persist data across navigation but here you're recreating the LiveData every time this method is called, meaning when you resume ConversationFragment and onViewCreated is called, it creates a new Pager which fetches new data.
The solution would be to create the pager when ConversationViewModel is first created and then accessing the LiveData object itself, rather than the method. You can see this in the Codelab example, they assign the LiveData in the constructor and simply return the already created LiveData in the getAllWords() method.
I'm using this as an example, change ConversationViewModel to something like this and change it to use your config and repository.
private final LiveData<PagingData<ItemView>> messageList;
public ConversationFragmentVM(#NonNull Application application) {
super(application);
repository = new Repository(application);
// CoroutineScope helper provided by the lifecycle-viewmodel-ktx artifact.
CoroutineScope viewModelScope = ViewModelKt.getViewModelScope(viewModel);
Pager<Integer, User> pager = Pager<>(
new PagingConfig(/* pageSize = */ 20),
() -> ExamplePagingSource(backend, query));
messageList = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), viewModelScope);
}
public LiveData<PagingData<ItemView>> getList(){
return messageList;
}
Then in your fragment, you simply observe getList() like usual, except this time it's returning a prexisting version.
viewModel.getList().observe(getViewLifecycleOwner(), pagingData ->
adapter.submitData(lifecycleOwner.getLifecycle(), pagingData));
}
I haven't been able to test that this compiles or works so let me know if it doesn't and I'll update this answer.
I have a conventional Room->DAO->Livedata->Repositiry->ViewModel->RecyclerView app. Different buttons of UI must pass different lists of data to RecyclerView.
By button click I want:
Make new #Query in DAO and get new LiveData<`List> object in return.
Put this new data into the RecyclerViewAdapter and call notifyDataSetChanged () to make new List visuals.
The Dao #Query:
#Query("SELECT * FROM entry_table WHERE path LIKE :path ORDER BY priority DESC")
LiveData<List<Entry>> getNotesOfFolder(String path); //Returns LiveData with List of Entries
The recyclerView is updated via onChanged of Observer like this:
public class RecyclerViewActivity extends AppCompatActivity {…
Observer<List<Entry>> entryObserver = new Observer<List<Entry>>() {
#Override
public void onChanged(List<Entry> entries) {
recyclerAdapter.setEntries(entries);
}
};
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.EntryHolder> {…
public void setEntries(List<Entry> entries) {
this.entries = entries; //setting LiveData content to adapter's List (i.e. entries)
notifyDataSetChanged();
The problem is that my Observer does not call the onChange method when LiveData receives new value from DAO. I believe it is because this LiveData’s content is not CHANGED but REPLACED by another LiveData.
I tried to re-subscribe the Observer to LiveData again and it somewhat worked, but when I try to call some conventional Room queries like #Delete, I got multiple (up to 10!) onChange calls and some of them behave weirdly and pass the wrong List to RVadapter.
So there two questions:
How can I just call onChanged() of my Observer?
Is there some other stylish way of passing new LiveData object to RecyclerView dynamically?
1)
In viewModel , create a getter method for live data:
//...
private LiveData<List<Entry>> liveData;
//...
public LiveData<List<Entry>> getLiveData() {
return liveData;
}
in Activity:
#Override
protected void onCreate(Bundle savedInstanceState) {
//...
viewModel.getLiveData().observe(this, new Observer<List<Entry>>() {
#Override
public void onChanged(List<Entry> entryList) {
//set new value here
}
});
}
2) DiffUtil is very helpful to update your list in recycler view and it gives you some nice animations.
I tried to re-subscribe the Observer to LiveData again and it somewhat worked, but when I try to call some conventional Room queries like #Delete, I got multiple (up to 10!) onChange calls and some of them behave weirdly and pass the wrong List to RVadapter.
This would make sense if you didn't first unsubscribe your observer from the old LiveData object... the one you replace when your query changes.
If your query updates, you will need to get a new LiveData from the DAO. If you overwrite your old LiveData with the new one, you will not only need to (re-)subscribe your Observer to the new one, you will also need to unsubscribe it from the old one. Otherwise it will live on and keep updating the observer.
For example, I have 12 UpcomingGamesFragment and each fragment has a different set of game data releasing a month, for example the first fragment of 12 will have video games releasing on January 2019.
In my app, there's a navigation drawer with a list of platforms (ps4, xbox, pc, etc.) and when the user picks his consoles by clicking on the check boxes and then closes the drawer layout, I want all the fragments to update accordingly. Like only show games releasing on these platforms. Retrieve and filter successfully works through a method in UpcomingGamesFragment called loadReleasesData()
Now what I want is all the fragments to update when the navigation drawer gets closed, because my implementation doesn't work, please tell me what's wrong.
Here's my ViewModel class:
public class ReleasesViewModel extends ViewModel {
private MutableLiveData<List<_Release>> upcomingReleases = new MutableLiveData<>();
public ReleasesViewModel() { }
public MutableLiveData<List<_Release>> getUpcomingReleases() {
return upcomingReleases;
}
}
And in my Filter drawer layout is in my MainActivity, and also I declare my ViewModel in my MainActivity:
OnCreate (A lot of code omitted for clarity)
mReleasesViewModel = ViewModelProviders.of(this).get(ReleasesViewModel.class);
My drawer layout on close:
#Override
public void onDrawerClosed(#NonNull View drawerView) {
// If change detected refresh!
if (!mCopyOfUserPlatforms.equals(SharedPrefManager.read(SharedPrefManager.PLATFORM_IDS, mDefaultPlatformsSet))) {
mReleasesViewModel.getUpcomingReleases().setValue(new ArrayList<_Release>());
}
}
And I pass the viewmodel livedata object to my 12 fragments when I initialize them in another fragment called the ViewPagerFragment here's how:
onCreateView:
// Get the ViewModel
ReleasesViewModel releasesViewModel = ((MainActivity)getActivity()).mReleasesViewModel;
for (int i = 0; i < 14; i++) {
UpcomingGamesFragment upcomingGamesFragment = new UpcomingGamesFragment();
upcomingGamesFragment.setLiveData(releasesViewModel); // HERE
mSectionsPagerAdapter.addFragment(upcomingGamesFragment, title, queryId);
}
This is my setLiveData() method in UpcomingGamesFragment:
public void setLiveData(ReleasesViewModel releasesViewModel) {
releasesViewModel.getUpcomingReleases().observe(this, new android.arch.lifecycle.Observer<List<_Release>>() {
#Override
public void onChanged(#Nullable List<_Release> releases) {
Log.d(TAG, "Refreshing this baby boy...");
loadReleaseData(0);
}
});
}
How I know the livedata doesn't update all my fragments? It is because I have a log in my loadReleasesData method and it doesn't get printed in the Logcat and not to mention the fact it doesn't update the fragment(s). Have a good day and bye! :)
Your issue is not with LiveData but with ViewModel instance you're getting in ViewPagerFragment :
use this in your onCreateView() of fragment:
ReleasesViewModel releasesViewModel = ViewModelProviders.of(getActivity()).get(ReleasesViewModel.class);
instead of your code:
// Get the ViewModel
ReleasesViewModel releasesViewModel = ((MainActivity)getActivity()).mReleasesViewModel;
I have followed this example, an integrated ViewModel and LiveData.
I used LiveData for ViewModel to Repository communication, and activity to ViewModel communication
I have few confusions that I want to clear this question.
This is working fine, and display Toast Message after 5 seconds on MainActivity.
MainActivity
MainViewModel homeViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
homeViewModel.getResponseval().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
Toast.makeText(getApplicationContext(), "MainActivityObserverCalled", Toast.LENGTH_LONG).show();
}
});
MainViewModel
public class MainViewModel extends ViewModel {
public MutableLiveData<String> responseval;
private LoginRepositry loginRepositry;
public MainViewModel(){
loginRepositry = new LoginRepositry();
responseval = loginRepositry.getData("username","password");
}
public MutableLiveData<String> getResponseval() {
return responseval;
}
LoginRepositry
public class LoginRepositry {
private MutableLiveData<String> data = new MutableLiveData<>();
public MutableLiveData<String> getData(String username , String userpass) {
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
data.setValue("Login Repo Set Value");
}
}, 5000);
return data;
}
These are my 2 questions.
Now with each method, I am returning some data of type LiveData, but
at the time of returning the data, the value is not set. I set it after 5
seconds data.setValue("SomeValue"), So what is the use of return
here, is it only because of method return type, and does nothing in
case of LiveData
In MainActivity, i used homeViewModel.getResponseval().observe
to observer data, but in ViewModel, I didn't use observe, but
ViewModel is still observing the Repository and after 5 seconds
MainActivity gets a result. I am unable to understand whats happening here
.
So let's do this by parts:
Now with each method i am returning some data of type LiveData, but at
the time of returning the data, the value is not set. I set it after 5
seconds data.setValue("SomeValue"), So what is the use of return here,
is it only because of method return type, and does nothing in case of
LiveData
If you check the docs you'll see that LiveData "is an observable data holder class", so it holds data and you can observer it. This is very important to understand why you're returning this LiveData object here. By returning it you're telling the next layer of your architecture (the ViewModel) "hey, here is this holder, I will put some data here at some point, so observer it if you want to receive the data".
The ViewModel doesn't observe it, but simply pass it to the next entity interested in this holder of data, the UI (LifeCycleOwner). So in the owner you start to observe this "holder of data" and will be warned when new data arrives.
In MainActivity, i used homeViewModel.getResponseval().observe to
observer data, but in ViewModel, I didn't use observe, but ViewModel
is still observing the Repository, and after 5 seconds MainActivity
gets result. I am unable to understand whats happening here.
You need a LifeCycleOwner in order to observe updates from a LiveData, also from the docs: "LiveData considers an observer, which is represented by the Observer class, to be in an active state if its lifecycle is in the STARTED or RESUMED state. LiveData only notifies active observers about updates." In order to detect the state, it needs a LifeCycleOwner, that's why when you have the observe method you pass this as a parameter. And that's why you didn't use observe in the ViewModel.
I have an Android app that uses a pretty common design pattern:
The main activity is essentially presenting a list of objects - on small devices it does so by hosting a single fragment that displays a recyclerview of this list. On larger devices it hosts two fragments, one which has the same recyclerview of objects, and another which will host the detail for individual objects when one is selected in the list.
On smaller devices, when a selection from the list is made, an activity is launched that hosts a fragment that utilizes a ViewPager to allow "swiping" through the list of objects, and edit each one in place.
In both cases, the user is allowed to edit only from the detail fragment.
I currently have my realm instance initialized in the application class, then the default instance retrieved in an activity base class I use to hold some housekeeping methods:
public abstract class SingleFragmentActivity extends AppCompatActivity {
private Realm realm;
protected abstract Fragment createFragment();
#LayoutRes
protected int getLayoutResId() {
return R.layout.activity_fragment;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
realm = Realm.getDefaultInstance();
// Initialize ProfileLab
ProfileLab.get(realm);
setContentView(getLayoutResId());
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_container);
if (fragment == null) {
fragment = createFragment();
fm.beginTransaction()
.add(R.id.fragment_container, fragment)
.commit();
}
}
#Override
protected void onDestroy() {
super.onDestroy();
if ( realm != null) {
realm.close();
}
}
}
Note that I am storing this instance of realm in a static class "ProfileLab":
// Initialize ProfileLab
ProfileLab.get(realm);
Then in the various fragments that update data, I am doing stuff like:
mProfile = ProfileLab.get().getProfile(profileId);
*
* do CRUD activities here for example:
*
private void deleteProfile() {
ProfileLab.get().deleteProfile(mProfile);
mCallbacks.onProfileUpdated(mProfile);
}
Then in ProfileLab, it looks like:
public boolean deleteProfile(Profile c) {
boolean retVal = true;
try {
mRealm.beginTransaction();
c.deleteFromRealm();
} catch (Exception e) {
retVal = false;
} finally {
if ( mRealm != null ) {
if (retVal) {
mRealm.commitTransaction();
} else {
mRealm.cancelTransaction();
}
}
}
return (retVal);
}
My question - is this a problem to essentially hold that Realm instance open like that throughout the use of the app? I noticed this paragraph in the docs:
If you get a Realm instance from a thread that does not have a Looper
attached, then objects from such instance will not be updated unless
the waitForChange() method is called. It is important to note that
having to hold on to an old version of your data is expensive in terms
of memory and disk space and the cost increases with the number of
versions between the one being retained and the latest. This is why it
is important to close the Realm instance as soon as you are done with
it in the thread.
The thing is, I am not 'done with it' because this is on the UI thread, which is obviously running throughout the lifetime of my app.
I can't really open/close the realm instance just for the atomic updates, because I need to use the result of the initial query to show the list of objects from which to choose to edit - when I tried that initially (I had realm object open/close within each method in ProfileLab itself) I got an error in my recycler adapters that the realm had been closed...
The example code showing use of the recycler view shows realm being retrieved/used/closed at the individual activity level, if I do that between say the two simple activities (hosting the RecyclerView and hosting the ViewPager), will the data updates be reflected in each other?
Opening and closing the realm within try/catch block is recommended. for an example:
try {
Realm realm = Realm.getDefaultInstance();
//Use the realm instance
}catch(Exception e){
//handle exceptions
}finally {
realm.close();
}
It's a basic example when going to use. If you can close within AsyncTask, it'll be better.
The official documentation refers that if you use minSdkVersion >= 19 and Java >= 7, you won't close it manually.
try (Realm realm = Realm.getDefaultInstance()) {
// No need to close the Realm instance manually
}
Realm will automatically keep Realms on Looper threads up to date. That particular line in the documentation mostly refers to background threads. So your code is fine, even if onDestroy might not be called.
You can also read these relevant sections in the docs:
https://realm.io/docs/java/latest/#closing-realms
https://realm.io/docs/java/latest/#realm-instance-lifecycle