问题
I have activity A and activity B in Android.
In activity A there are a FirestoreRecyclerAdapter and a FirestoreRecyclerOptions object, which update in realtime a RecyclerView. I start activity B from a button in activity A, but I do not finish Activity A, so that when returning to Activity A I do not need to read again all documents already previously loaded within my recyclerView.
However, if I start a new intent from activity B to activity A, what happens to the Firestore listener in the old Activity A? Is it automatically terminated?
回答1:
When you are using Query's addValueEventListener() method, you should remove the listener according to the life-cycle of your activity, as explained in my answer from the following post:
- Should I actually remove the ValueEventListener?
If you are using the Firebase-UI library, once you start listening for changes in your onStart() method:
@Override
protected void onStart() {
super.onStart();
firestoreRecyclerAdapter.startListening();
}
You also need to stop listening for changes in onStop() method:
@Override
protected void onStop() {
super.onStop();
if(firestoreRecyclerAdapter != null) {
firestoreRecyclerAdapter.stopListening();
}
}
来源:https://stackoverflow.com/questions/55832855/managing-the-android-back-stack-and-listeners-with-firestore-for-optimizing-docu