how to store recyclerview data on onSaveInstanceState - android

I want to save data from the API in the RecyclerView so that when rotating the screen is not reloaded
I think I can use onSaveInstanceState but still don't really understand how to use it
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final RecyclerView rvTVShow = view.findViewById(R.id.rv_shows);
rvTVShow.setHasFixedSize(true);
rvTVShow.setLayoutManager(new LinearLayoutManager(getActivity()));
ApiInterface apiService =
ApiClient.getClient().create(ApiInterface.class);
Call<MovieResponse> call = apiService.getTVShow(API_KEY);
call.enqueue(new Callback<MovieResponse>() {
#Override
public void onResponse(#NonNull Call<MovieResponse> call, #NonNull Response<MovieResponse> response) {
final List<Movies> movies = Objects.requireNonNull(response.body()).getResults();
TvShowAdapter tvShowAdapter = new TvShowAdapter(movies , R.layout.list_movies);
rvTVShow.setAdapter(tvShowAdapter);
....
}

I will explain how savedInstanceState works while refactoring your code.
First: Create a global Movie object and an Adapter for it
List<Movies> movies = new ArrayList();
TvShowAdapter tvShowAdapter = null;
Re-initialize adapter under activity onCreate
tvShowAdapter = new TvShowAdapter(movies , R.layout.list_movies);
rvTVShow.setAdapter(tvShowAdapter);
Create a new method to handle movie
data population
public void populateRV(List<Movies> movies)
{
this.movies = movies;
//notify adapter about the new record
tvShowAdapter.notifyDataSetChanged();
}
Insert data to Movies object under your Response callback
movies = Objects.requireNonNull(response.body()).getResults();
populateRV(movies);
Everytime the orientation of an activity changes Android resets the states of all views by redrawing them. This causes non persistent data to be lost. But before redrawing views it calls the method onSavedInstanceState
Hence we can prevent state loss by saving the state of our views using the already defined onSavedInstanceState method provided by android.
Add the following block inside the overridden onSavedInstanceState method of your activity
//this saves the data to a temporary storage
savedInstanceState.putParcelableArrayList("movie_data", movies);
//call super to commit your changes
super.onSaveInstanceState(savedInstanceState);
Next is to recover the data after orientation change is completed
Add the following block in your activity onCreate and make sure it comes after initializing your adapter
//...add the recyclerview adapter initialization block here before checking for saved data
//Check for saved data
if (savedInstanceState != null) {
// Retrieve the data you saved
movies = savedInstanceState.getParcelableArrayList("movie_data");
//Call method to reload adapter record
populateRV(movies);
} else {
//No data to retrieve
//Load your API values here.
}

Related

How to fix a List<> that become null inside OnCreateView()

I am trying to retrieve data from Firebase realtime-database and put it on a CardView inside a RecyclerView inside a Fragment.
But the Fragment shown is blank white with no error. I retrieve the data inside OnCreate method and add it into a List.
While debugging the application, found out that even after assigning the retrieved data inside the onCreate method, the list is still NULL inside the onCreateView method.
Fragment Dashboard List Class:
public class fragment_dashboard_list extends Fragment {
List<ibu> ibu_ibu;
FirebaseDatabase database;
DatabaseReference myRef ;
String a;
public fragment_dashboard_list() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ibu_ibu = new ArrayList<>();
database = FirebaseDatabase.getInstance();
myRef = database.getReference("Guardian");
myRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
ibu value = dataSnapshot1.getValue(ibu.class);
ibu ibu_val = new ibu();
String alamat = value.getAlamat();
String foto = value.getFoto();
String hp = value.getHp();
String ktp = value.getKtp();
String nama = value.getNama();
String privilege = value.getPrivilege();
String ttl = value.getTtl();
ibu_val.setAlamat(alamat);
ibu_val.setFoto(foto);
ibu_val.setHp(hp);
ibu_val.setKtp(ktp);
ibu_val.setNama(nama);
ibu_val.setPrivilege(privilege);
ibu_val.setTtl(ttl);
// Here the List ibu_ibu is not NULL
ibu_ibu.add(ibu_val);
}
}
#Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w("Hello", "Failed to read value.", error.toException());
}
});
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_dashboard_list, container, false);
RecyclerView myrv = (RecyclerView) view.findViewById(R.id.dashboard_recycler_view);
//Here the List ibu_ibu is null
adapter_list_ibu myAdapter = new adapter_list_ibu(ibu_ibu);
LinearLayoutManager LinearLayoutManager = new LinearLayoutManager(getContext());
myrv.setLayoutManager(LinearLayoutManager);
myrv.setAdapter(myAdapter);
return view;
}
}
I expected the List to be not NULL inside OnCreateView so the Fragment wont Blank
Firebase APIs are asynchronous, which means that onDataChange() method returns immediately after it's invoked, and the callback will be called some time later. There are no guarantees about how long it will take. So it may take from a few hundred milliseconds to a few seconds before that data is available.
Because that method returns immediately, your ibu_val list that you're trying to use it outside the onDataChange() method, will not have been populated from the callback yet and that's why is always empty.
Basically, you're trying to use a value of variable synchronously from an API that's asynchronous. That's not a good idea, you should handle the APIs asynchronously as intended.
A quick solve for this problem would be to notify the adapter once you got all elements from the database using:
myrv.notifyDatasetChanged();
So add this line of code right after where the for loop ends.
If you intent to use that list outside the callback, I recommend you see the last part of my anwser from this post in which I have explained how it can be done using a custom callback. You can also take a look at this video for a better understanding.

How to bind live data to array adapter android

I have a simple application while I'm trying to understand android/room. I would like to have my query from room to be placed into my list view.
PersonDao.class
#Query("Select name from People limit 3")
LiveData<List<String>> getThreeNames();
AvtivityMain.class
private ArrayAdapter<String> adapter;
private PersonDatabase db;
private EditText age;
private EditText name;
Person person;
private DatabaseRepository rDb;
private PersonViewModel personViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
age = findViewById(R.id.age);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
ListView lvPerson = findViewById(R.id.lv3Poeple);
lvPerson.setAdapter(adapter);
personViewModel = ViewModelProviders.of(this).get(PersonViewModel.class);
}
public void addPerson(View view){
int sAge = Integer.parseInt(age.getText().toString());
String sName = name.getText().toString();
person = new Person(sName, sAge);
personViewModel.insert(person);
System.out.println(personViewModel.getmAllPeople());
System.out.println(personViewModel.getm3People());
//List<String> names = personViewModel.getm3People();
//adapter.add(personViewModel.getm3People());
}
I have commented out the code I am having problems with. I want to be able to use my query from PersonDao and have my list view show 3 names from my Room database.
You need to do a few things, the first is make sure your adapter can take new input. I would suggest creating your own custom one. When you do create it, be sure to include a method that takes your person strings as input and notifies the adapter of the change, like so:
//you need to add an update method to your adapter, so it can change it's data as your
//viewmodel data changes, something like this:
public void setPersons(List<String> personNames) {
//myPersons should be a variable declared within your adapter that the views load info from
myPersons = personNames;
notifyDataSetChanged();
}
Then in your activity you should set the Adapter as a global variable so it can be accessed by multiple methods, such as your view model observer method and it's initial setup in onCreate().
//need to set up adapter variable to access in other methods to keep view model observer
//off the main thread
private ArrayAdapter myAdapter;
After that you can set up the following observer method for your ViewModel (assuming you created the ViewModel Class properly with a return method called getThreeNames() and that isn't just within your DAO as you showed above.
//set up the view model observer to be off the main thread so it isn't tied to your main
//activity lifecyle (which is the whole point/beauty of the ViewModel), you can call this
//method in your onCreate() method and should stay until onDestroy() is called
private void setupPersonViewModel() {
PersonViewModel viewModel
= ViewModelProviders.of(this).get(PersonViewModel.class);
viewModel.getThreeNames().observe(this, new Observer<List<String>>() {
#Override
public void onChanged(#Nullable List<String> persons) {
//so you can see when your app does this in your log
Log.d(TAG, "Updating list of persons from LiveData in ViewModel");
mAdapter.setPersons(persons);
}
});
}
Hope that answers your question. Let me know if you need any further clarification.

How to properly restore data when resuming an activity using Firebase Database?

I'm developing an application that display a list of items and allows the users to add more items and check the items' details.
Let's say that I keep my database references in the activity, adding ChildEventListener listeners onResume() and removing them on onPause():
public void onResume() {
super.onResume();
mItemChildEventListener = mDatabaseReference.child('items').addChildEventListener(new ChildEventListener() {
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
mAdapter.addItem(dataSnapshot.getValue(Item.class));
}
...
};
}
public void onPause() {
super.onPause();
if (mItemChildEventListener != null) {
mDatabaseReference.removeEventListener(mItemChildEventListener);
}
}
In these ChildEventListener, I'm adding item by item to the list of items and calling notifyItemInserted(itemArray.size() - 1) in the adapter.
public void addItem(Item item) {
if (mItems == null) {
mItems = new ArrayList<>();
}
mItems.add(item);
notifyItemInserted(mItems.size() - 1);
}
Also, to keep the application running smoothly, I'm keeping the list of items in a Reteiner Fragment, saving this values onSaveInstanceState() and restoring them on onCreate().
protected void onCreate(#Nullable Bundle savedInstanceState) {
if (savedInstanceState != null) {
mItems = getState(STATE_ITEMS);
} else {
mItems = new ArrayList<>();
}
mAdapter = new ItemAdapter(mItems);
}
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
saveState(STATE_ITEMS, mItems);
}
public <T> T getState(String key) {
//noinspection unchecked
return (T) mRetainedFragment.map.get(key);
}
public void saveState(String key, Object value) {
mRetainedFragment.map.put(key, value);
}
public static class RetainedFragment extends Fragment {
HashMap<String, Object> map = new HashMap<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// retain this fragment
setRetainInstance(true);
}
}
However, if I unregister and register the listener again, for example, by any orientation change or leaving and returting to the activity, as expected, onResume() will be called, and by adding the ChildEventListener again, the same data is retrieved, which, in case if it's not compared with the data that is already contained in the list of items, mItems, it will be duplicated.
Also, if I do not keep the list of items, mItems, and retrieve the data every time onResume() is called, the list loses its scroll position and also blinks, since it takes some time to retrieve the data.
Question
How can I keep the list of items retrieved without retrieving them again again, but keep listening for changes in the items already retrieved and for new items added? Is there a better solution than comparing the items already retrieved with the new items, for example, by their key?
I've tried looking in the documentation and other questions, but I couldn't find any information relevant for this situation. Also, I have tried to keep the reference to mDatabaseReference in onSaveInstanceState(), as I have done with the list of items mItems, but it has't worked either.
If you enable persistence in the Firebase Realtime Database SDK, that will help prevent unnecessary fetches of data that's already been received. The SDK will cache fetch data locally and prefer to use that data first when it's available:
FirebaseDatabase.getInstance().setPersistenceEnabled(true);
Be sure to read the documentation to understand how this works.
Also bear in mind that it's traditional to start and stop listener during onStart and onStop. This will avoid even more refetches if the app loses focus for a moment (for example, it pops up a dialog, or some other transparent activity appears on top).
Just make a Firebase call inside a Loader and save the data in a static variable or list or whatever data structure suits you.
Initialize the Loader in onCreate.
In the Loader, override the onStartLoading() method and in onStart() call this method.
Inside onStartLoading() simply check if the static variable is null or not. If it is null, startLoading. Else do not load, and set the previous data as data source.
The advantage of using Loaders is, in case of orientation changes, it won't make network calls as AsyncTask does.

Best way to change data on FirebaseRecyclerAdapter

currently I am using FirebaseRecyclerAdapter to represent data on a RecyclerView using the following Firebase query:
postsQuery = mDatabase.child("lists_new”).orderByKey().limitToFirst(10);
My RecyclerView has a header with 2 buttons: New List, Old List.
New list is loaded by default, and my question is, when the user taps the Old List button, what is the most efficient way to replace the new list with old list.
The Firebase query for the old list looks like this:
postsQuery = mDatabase.child("lists_old”).orderByKey().limitToFirst(10);
Note: both new list and the old list has the same data types, i.e. they share the same Java POJO class and the the same layout.
You will need a new adapter and attach that to the same RecyclerView.
So after constructing the new query, you create a new adapter and attach it to the view:
adapter = new FirebaseRecyclerAdapter<Chat, ChatHolder>(
Chat.class, android.R.layout.two_line_list_item, ChatHolder.class, postsQuery) {
recyclerView.setAdapter(adapter);
I have a similar need and a similar solution except I am calling cleanup() on the adapter before new'ing up another one. I am thinking without calling cleanup() it will create a leak of adapters and/or listeners?
In onActivityCreated() in my Fragment I am calling a method in the Fragment that manages the recycler view. Call the method to initialize or refresh the list and pass in a leaf node name. If the adapter is not null then call cleanup(). Create a new database reference by concatenating a new leaf node with the parent reference, new-up a new adapter and set it.
I call cleanup() in onDestroy() as well, per usual.
It works fine so far though I've only tested using the emulator and a small data set.
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
refreshList(newLeafNode);
}
#Override
public void onDestroy() {
super.onDestroy();
mRVAdapter.cleanup();
}
protected void refreshList(String newLeafNode) {
if (mRVAdapter != null) {
mRVAdapter.cleanup();
}
DatabaseReference newDbRef = mParentRef.child(newLeafNode);
mRVAdapter = new FirebaseRecyclerAdapter<String, MyViewHolder>(String.class, R.layout.single_row, MyViewHolder.class, newDbRef) {
#Override
protected void populateViewHolder(MyViewHolder viewHolder, String model, int position) {
viewHolder.setImage(model);
}
};
mMyRV.setAdapter(mRVAdapter);
}

ArrayList Length gets 0 in Singleton

I am using a singleton for fetching data from a web service and storing the resulting data object in an ArrayList. It looks like this:
public class DataHelper {
private static DataHelper instance = null;
private List<CustomClass> data = null;
protected DataHelper() {
data = new ArrayList<>();
}
public synchronized static DataHelper getInstance() {
if(instance == null) {
instance = new DataHelper();
}
return instance;
}
public void fetchData(){
BackendlessDataQuery query = new BackendlessDataQuery();
QueryOptions options = new QueryOptions();
options.setSortBy(Arrays.asList("street"));
query.setQueryOptions(options);
CustomClass.findAsync(query, new AsyncCallback<BackendlessCollection<CustomClass>>() {
#Override
public void handleResponse(BackendlessCollection<CustomClass> response) {
int size = response.getCurrentPage().size();
if (size > 0) {
addData(response.getData());
response.nextPage(this);
} else {
EventBus.getDefault().post(new FetchedDataEvent(data));
}
}
#Override
public void handleFault(BackendlessFault fault) {
EventBus.getDefault().post(new BackendlessFaultEvent(fault));
}
});
}
public List<CustomClass> getData(){
return this.data;
}
public void setData(List<CustomClass> data){
this.data = data;
}
public void addData(List<Poster> data){
this.data.addAll(data);
}
public List<CustomClass> getData(FilterEnum filter){
if(filter == FilterEnum.NOFILTER){
return getData();
}else{
// Filtering and returning filtered data
}
return getData();
}
}
The data is fetched correctly and the list actually contains data after it. Also, only one instance is created, as intended. However, whenever I call getData later, the length of this.data is 0. Because of this I also tried it with a subclass of Application holding the DataHelper object, resulting in the same problem.
Is there a good way of debugging this? Is there something like global watches in Android Studio?
Is there something wrong with my approach? Is there a better approach? I am mainly an iOS developer, so Android is pretty new to me. I am showing the data from the ArrayList in different views, thus I want to have it present in an the ArrayList as long as the application runs.
Thanks!
EDIT: Example use in a list view fragment (only relevant parts):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
filter = FilterEnum.NOFILTER;
data = DataHelper.getInstance().getData(filter);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
customClassListAdapter = new customClassListAdapter(getActivity(), data);}
EDIT2: Added code where I fetch the data from Backendless, changed reference of DataHelper to reference of data in first EDIT
EDIT3: I usa a local EventBus for notifying the list view about the new data. This looks like this and works (initially the data gets populated, but after e.g. applying a filter, the ArrayList I get with getData is empty):
#Subscribe
public void onMessageEvent(FetchedDataEvent event) {
customClassListAdapter.notifyDataSetChanged();
}
Try instead of keeping reference to your DataHelper instance, keeping reference to your list of retrieved items. F.e. when you first fetch the list (and it's ok as you say), assign it to a class member. Or itarate through it and create your own array list of objects for future use.
Okay I finally found the problem. It was not about the object or memory management at all. Since I give the reference on getData to my ArrayAdapter, whenever I call clear (which I do when changing the filter) on the ArrayAdapter, it empties the reference. I basically had to create a copy of the result for the ArrayAdapter:
data = new ArrayList<>(DataHelper.getInstance().getData(filter));
I was not aware of the fact that this is a reference at all. So with this the data always stays in the helper entirely. I only did this because this:
customClassListAdapter.notifyDataSetChanged();
does hot help here, it does not call getData with the new filter again.
Thanks everyone for your contributions, you definitely helped me to debug this.
It is likely that getData does get called before the data is filled.
A simple way to debug this is to add (import android.util.Log) Log.i("MyApp.MyClass.MyMethod", "I am here now"); entries to strategic places in fetchData, addData and getData and then, from the logs displayed by adb logcat ensure the data is filled before getData gets called.

Categories

Resources