[英]Firebase how to ValueEventListeners remove when other activity open in Android
I'm editing the question for more clarity为了更清楚,我正在编辑问题
In my Activity A
, I have have started my new Activity B
for certain condition.在我的Activity A
中,我已经在某些情况下开始了我的新Activity B
。 OnCreate of Activity A has the following Activity A 的 OnCreate 有以下内容
final DatabaseReference reff = FirebaseDatabase.getInstance().getReference("Example").child(userid);
reff.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {`
navHeaderName.setText(Objects.requireNonNull(dataSnapshot.child("name").getValue()).toString());
navHeaderId.setText(Objects.requireNonNull(dataSnapshot.child("member_ID").getValue()).toString());
} else
reff.removeEventListener(this);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("RF-NavSet", databaseError.toString());
}
});
` `
In the fragment Af
is also si inflated in Activity A
在fragment Af
也在Activity A
中膨胀
FragmentManager fm = getSupportFragmentManager();
fragment = new FragmentOfActiityA();
fm.beginTransaction().replace(R.id.doc_fragment_container, fragment).commit();
In Fragment A also I have Firebase ValueEventListener在片段 A 中,我也有 Firebase ValueEventListener
listener = new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
TextView text1 = view.findViewById(R.id.some_text1);
TextView text2 = view.findViewById(R.id.some_text2);
TextView text3 = view.findViewById(R.id.some_text3);
text1.setText(Objects.requireNonNull(dataSnapshot.child(userId).child("name").getValue()).toString());
text2.setText(Objects.requireNonNull(dataSnapshot.child(userId).child("specialisation").getValue()).toString());
text3.setText(Objects.requireNonNull(dataSnapshot.child(userId).child("member_ID").getValue()).toString());
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("RF - SetProfile", databaseError.getMessage());
}
};
reff.addListenerForSingleValueEvent(listener);
Here reff
and listener
are globally declared.这里reff
和listener
是全局声明的。
When I destroy Activity A, I want to remove listener in Activity A
and Fragment Af
.当我销毁 Activity A 时,我想删除Activity A
和Fragment Af
中的侦听器。
How can I do that?我怎样才能做到这一点?
If I change any database value from some other Activity X
, Activity A
is automatically triggered due to these ValueEventListener which I dont want.如果我从其他一些Activity X
更改任何数据库值,由于这些我不想要的 ValueEventListener 会自动触发Activity A
Remove the listener in your onDestroy()
of activity A
删除活动A
的onDestroy()
中的侦听器
mDatabaseRef.child(key).removeListener(mListener);
And your activity A
in background when open activity B
and if you want to remove listener then remove listener in onPause()
and add listener in onResume()
当打开活动B
时,您的活动A
在后台,如果您想删除侦听器,则在onPause()
中删除侦听器并在onResume()
中添加侦听器
I have got the answer for the problem.我已经得到了问题的答案。 There one previous Activity O
which I missed to removeEventListener which was secretly launching Activity A
.有一个之前的Activity O
我错过了 removeEventListener ,它正在秘密启动Activity A
。
Took a while to understand that花了一段时间才明白
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.