问题描述
为了从 Firebase 实时数据库接收数据,我需要注册监听器:
For receiving data from Firebase Realtime Database I need to register listener:
objectReference.addValueEventListener(valueEventListener);
删除(注销)此侦听器的正确方法是什么?
What is correct way to remove (unregister) this listener?
推荐答案
删除侦听器的正确方法是使用以下代码行根据活动的生命周期将其删除:
The correct way to remove a listener is to remove it accordingly to the life-cycle of your activity using this line of code:
databaseReference.removeEventListener(valueEventListener);
请注意,如果您在 onStart
中添加了侦听器,则必须在 onStop
中将其删除.如果您在 onResume
中添加了侦听器,则必须在 onPause
中将其删除.如果您在 onCreate
中添加了侦听器,则必须在 onDestroy
中将其删除.
Note that, if you have added the listener in onStart
you have to remove it in onStop
. If you have added the listener in onResume
you have to remove it in onPause
. If you have added the listener in onCreate
you have to remove it in onDestroy
.
但请记住 onDestroy
并不总是被调用.
But remember onDestroy
is not always called.
这篇关于如何从 Firebase 实时数据库中删除侦听器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!