yot*_*das 5 android firebase firebase-realtime-database
如果我以下列方式在活动中使用侦听器:
// Read from the database
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// This method is called once with the initial value and again
// whenever data at this location is updated.
String value = dataSnapshot.getValue(String.class);
Log.d(TAG, "Value is: " + value);
}
@Override
public void onCancelled(DatabaseError error) {
// Failed to read value
Log.w(TAG, "Failed to read value.", error.toException());
}
});
Run Code Online (Sandbox Code Playgroud)
附加匿名侦听器(未附加到变量的事件),我还需要删除它吗?
*我将onStart()它设置在并且需要它运行直到onStop()/onDestroy()
什么时候需要移除监听器?
如果您只希望侦听器在活动处于活动状态时工作,则可以通过调用 Firebase 数据库引用上的removeEventListener() 方法来分离侦听器。如果您在 onStart() 中附加侦听器,那么您应该在 onStop() 中分离。
@Override
protected void onStop() {
super.onStop();
//...
myRef.removeEventListener();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1909 次 |
| 最近记录: |