I am following this question
how to store recyclerview data on onSaveInstanceState
I also found How to save state of view class? and Fragment save view state.
Context
I give data in form of DataModel
(implements Parcelable) to recyclerview
in one of my Fragments.
using Bottom navifation and ROOM DB
(to get and save Data).
what I have done yet
I used the code in first link and in my code. But I couldn't understand the fourth peace of code, which was used in there (I don't have a response.boddy()
, Error).
Anyway every time changing the view savedInstanceState = null
so the code is being redone.
what I want or question
I would like to not redo the work every time changing the view via bottom navigation?
what am I doing wrong, that data are not being saved in savedInstanceState
?
my Fragment view
private ArrayList<DataModel> data;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
root = inflater.inflate(R.layout.fragment_home, container, false);
currentContext = getContext();
recyclerView = (RecyclerView) root.findViewById(R.id.recyclerViewHome);
recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
this.data = new ArrayList<>();
adapter = new CustomAdapter(data, currentContext, 1);
recyclerView.setAdapter(adapter);
if (savedInstanceState != null) {
// Retrieve the data you saved
data = savedInstanceState.getParcelableArrayList("saved_data");
//Call method to reload adapter record
recyclerViewsaveInstance(data);
}
else {
//No data to retrieve
dataAsynTask; //deleted, Basicly I get the data from DB, convert it to DataModel and give them to recyclerview.
}
return root;
}
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
Log.i("savedInstanceState", "loading");
savedInstanceState.putParcelableArrayList("saved_data", this.data);
super.onSaveInstanceState(savedInstanceState);
}
public void recyclerViewsaveInstance(ArrayList<DataModel> dataset)
{
this.data = dataset;
adapter = new CustomAdapter(dataset, getContext(), 1);
recyclerView.setAdapter(adapter);//notify adapter about the new record
adapter.notifyDataSetChanged();
}