Managing the Android back stack and listeners with Firestore for optimizing document reads

僤鯓⒐⒋嵵緔 提交于 2020-01-11 07:44:21

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!