I have a fragment in an activity that uses Firestore snapshot listener. When I first start the fragment, it works fine. But then if I navigate to another fragment and return, I get an empty snapshot. How do I get it to fetch the snapshot properly again?
Here is the query with the listener registration:
private void setupRecyclerView() {
if (mealplanRecycler == null) {
mealplanRecycler = mealplanView.findViewById(R.id.mealplan_recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mealplanContext);
linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
mealplanRecycler.setHasFixedSize(true);
mealplanRecycler.setLayoutManager(linearLayoutManager);
}
getMealplans();
}
private void getMealplans() {
String userId = (bundle != null && bundle.containsKey("userId")) ? bundle.getString("userId") : "";
Query mealplanQuery = firebaseFirestore.collection("Meal_Plans").whereEqualTo("userId", userId).orderBy("timeOfCreation", Query.Direction.DESCENDING);
registration = mealplanQuery.addSnapshotListener((value, error) -> {
toolbarProgressbar.setVisibility(View.GONE);
mealplanArrayList = new ArrayList<>();
if (error == null && value != null) {
for (QueryDocumentSnapshot document : value) {
Mealplan mealplan = document.toObject(Mealplan.class);
Mealplan updatedmealplan = updateTime(mealplan);
mealplanArrayList.add(updatedmealplan);
if (mealplanArrayList.size() > 0) {
tvAddMealplan.setVisibility(View.GONE);
ivAddmealplan.setVisibility(View.GONE);
}
}
if (mealPlanAdapter != null) {
mealPlanAdapter.setItems(mealplanArrayList);
mealPlanAdapter.notifyDataSetChanged();
} else {
String planId = (bundle != null && bundle.containsKey("planId")) ? bundle.getString("planId") : "none";
mealPlanAdapter = new MealPlanAdapter(mealplanContext, mealplanArrayList, planId);
mealplanRecycler.setAdapter(mealPlanAdapter);
}
mealPlanAdapter.setEditMealplanListener(this::onButtonPressed);
} else {
Log.d(TAG, "error is not null: " + Objects.requireNonNull(error).getLocalizedMessage());
toolbarProgressbar.setVisibility(View.GONE);
tvAddMealplan.setVisibility(View.GONE);
}
});
}
And this is how I am creating the fragment in Main Activity:
MealplanFragment fragment = new MealplanFragment();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right, R.anim.enter_from_right, R.anim.exit_to_right).replace(R.id.frame_container, fragment, null);
fragmentTransaction.commit();
question from:
https://stackoverflow.com/questions/65648505/snapshot-listener-returning-empty-snapshot-when-i-open-the-fragment-again 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…