'Save recyclerView state between fragments on bottom navigation view
When i'm switching from another fragment to home fragment, the fragment loads again the data to my recycler view. I want to save the recycler view state and restoring it when the user switch to my home fragment. My problem is that i don't know how to save a custom array list to onSaveInstanceState() and then retrieve this from the onCreateView() method.
My HomeFragment:
public class HomeFragment extends Fragment {
LoadingDialog progressDialog;
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
boolean isSearchOpen = false;
private EditText searchView;
ArrayList<PostModel> postModelArrayList;
RecyclerView recyclerView;
void loadPosts(View fragmentView, FirebaseUser user, ArrayList<PostModel> postModelArrayList, RecyclerView.Adapter postsAdapter, boolean isRefresh) {
rootRef.addListenerForSingleValueEvent(new ValueEventListener() {
@SuppressLint("NotifyDataSetChanged")
@Override
public void onDataChange(@NonNull @NotNull DataSnapshot snapshot) {
new Handler().postDelayed(() -> {
fragmentView.findViewById(R.id.constraintLayout2).setVisibility(View.GONE);
((HomeActivity)getContext()).findViewById(R.id.bottom_nav).setVisibility(View.VISIBLE);
}, 2000);
for (DataSnapshot snap : snapshot.child("posts").getChildren()) {
PostModel postModel = new PostModel();
postModel.setId(snap.child("id").getValue(String.class));
postModel.setImgUrl(snap.child("imgUrl").getValue(String.class));
postModel.setLikes(snap.child("likes").getValue(String.class));
postModel.setName(snap.child("name").getValue(String.class));
postModel.setProfileImgUrl(snap.child("authorProfilePictureURL").getValue(String.class));
postModel.setPostType(snap.child("postType").getValue(String.class));
// Show post in recycler adapter only if the user is not blocked
if (!snapshot.child("users").child(user.getUid()).child("blockedUsers").child(snap.child("name").getValue(String.class)).exists()) {
postModelArrayList.add(postModel);
}
}
if (snapshot.child("users").child(user.getUid()).child("blockedUsers").exists()) {
System.out.println(snapshot.child("users").child(user.getUid()).child("blockedUsers"));
}
postsAdapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull @NotNull DatabaseError error) {
Toast.makeText(getContext(), "Error: " + error, Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
// i don't know what i need to do here
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_home, container, false);
TextView usernameLoadingScreen = view.findViewById(R.id.textView40);
ImageButton searchUserBtn = view.findViewById(R.id.searchPersonButton);
ImageButton notificationsBtn = view.findViewById(R.id.notificationsButton);
ImageButton searchUserButton = view.findViewById(R.id.enter_search_button);
View postsOfTheMonthBtn = view.findViewById(R.id.posts_of_the_month_btn);
searchView = view.findViewById(R.id.search_view);
DatabaseReference usersRef = FirebaseDatabase.getInstance().getReference("users");
FirebaseAuth auth = FirebaseAuth.getInstance();
FirebaseUser user = auth.getCurrentUser();
AdView mAdView = view.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
recyclerView = view.findViewById(R.id.home_recycler_view);
postModelArrayList = new ArrayList<>();
final LinearLayoutManager layoutManager = new LinearLayoutManager(getContext());
final RecyclerView.Adapter recyclerAdapter = new PostRecyclerAdapter(postModelArrayList, getContext(), getActivity());
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
recyclerView.setAdapter(recyclerAdapter);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(layoutManager);
progressDialog = LoadingDialog.Companion.get(getActivity());
searchView.setVisibility(View.GONE);
searchUserButton.setVisibility(View.GONE);
((HomeActivity)getContext()).findViewById(R.id.bottom_nav).setVisibility(View.GONE);
loadPosts(view, user, postModelArrayList, recyclerAdapter, false);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
