使用Firestore管理Android后向堆栈和侦听器以优化文档读取 [英] Managing the Android back stack and listeners with Firestore for optimizing document reads

查看:49
本文介绍了使用Firestore管理Android后向堆栈和侦听器以优化文档读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Android中有活动A和活动B.

I have activity A and activity B in Android.

活动A中有一个FirestoreRecyclerAdapterFirestoreRecyclerOptions对象,它们实时更新RecyclerView.我从活动A中的按钮开始活动B,但是我没有完成活动A,因此,当返回到活动A时,不需要再次读取先前已在recyclerView中加载的所有文档.

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.

但是,如果我从活动B到活动A启动新的意图,那么旧活动A中的Firestore侦听器会如何处理?它会自动终止吗?

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?

推荐答案

使用Query的

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:

如果您使用的是Firebase-UI库,则一旦开始侦听onStart()方法中的更改,就可以:

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();
}

您还需要停止监听onStop()方法中的更改:

You also need to stop listening for changes in onStop() method:

@Override
protected void onStop() {
    super.onStop();
    if(firestoreRecyclerAdapter != null) {
        firestoreRecyclerAdapter.stopListening();
    }
}

这篇关于使用Firestore管理Android后向堆栈和侦听器以优化文档读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆