So I've used the same method in Fragment to other Fragments but only the first Fragment
which I got it working saved its state and items inside the Adapter
.
Here's the code in onViewCreated
checking whether the Adapter
is null and if not, use the same old adapter :
if(getArguments() != null){
Parcelable savedRecyclerLayoutState = getArguments().getParcelable(utilities.BUNDLE_RECYCLER_LAYOUT);
if (getArguments().getString("finishPost") != null && getArguments().getString("finishPost").equals("true")){
refreshView();
lobiProgressBar.setVisibility(View.VISIBLE);
} else {
if(savedRecyclerLayoutState == null){
lobiAdapter = new LobiAdapter(this.getActivity());
lobiAdapter.setDisplay(width, height);
recyclerView.setAdapter(lobiAdapter);
refreshView();
} else {
if (lobiAdapter != null && lobiAdapter.getItemCount() > 0) {
manager.onRestoreInstanceState(savedRecyclerLayoutState);
if(lobiAdapter == null){
lobiAdapter = new LobiAdapter(this.getActivity());
lobiAdapter.setDisplay(width, height);
}
recyclerView.setAdapter(lobiAdapter);
lobiProgressBar.setVisibility(View.GONE);
} else /*if (lobiAdapter != null && lobiAdapter.getItemCount() == 0)*/{
refreshView();
lobiProgressBar.setVisibility(View.VISIBLE);
}
}
}
} else {
refreshView();
lobiProgressBar.setVisibility(View.VISIBLE);
}
And here's my onPause()
method in the same Fragment
which saves RecyclerView
's state :
@Override
public void onPause() {
super.onPause();
getArguments().putParcelable(utilities.BUNDLE_RECYCLER_LAYOUT, recyclerView.getLayoutManager().onSaveInstanceState());
if(getActivity().getSupportFragmentManager().getBackStackEntryCount() > 1){
((NewsfeedActivity)getActivity()).hideToolbarBottom();
}
}
Weird thing is, it saves the adapter's values, but I don't know when or how. When I debug it, lobiAdapter
shows that it has items in it, whereas, other Fragments
which uses same method of saving state has no item.
Am I actually doing it wrong or I am unaware of something which I've done to save the state and items?