我的应用中存在问题。我正在调用valueEventListener,我希望当用户关闭活动或启动另一个活动时停止监听,所以我调用removeEventListener但它无法正常工作
//Firebase
private DatabaseReference mDatabase;
private FirebaseUser mUser;
//Firebase
mUser = FirebaseAuth.getInstance().getCurrentUser();
user_id = mUser.getUid();
mDatabase = FirebaseDatabase.getInstance().getReference();
mDatabase.keepSynced(true);
listener =mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
url = dataSnapshot.child(CHILD_PIC).child(pic).getValue().toString();
player = dataSnapshot.child(CHILD_PLAYERS).child(players).getValue().toString();
Img_check = dataSnapshot.child(CHILD_PIC).child(CHILD_CHECK).getValue().toString();
balls = dataSnapshot.child("Users").child(user_id).child("Balls").getValue().toString();
number_miss = dataSnapshot.child("Users").child(user_id).child("Mission").getValue().toString();
Isso = dataSnapshot.child("Users").child(user_id).child("Isso").getValue().toString();
Tx = dataSnapshot.child("Users").child(user_id).child("Tx").getValue().toString();
Three = dataSnapshot.child("Users").child(user_id).child("Three").getValue().toString();
//verify = dataSnapshot.child(check).child(user_id).child(CHILD_CHECK).getValue().toString();
// Toast.makeText(PlayActivity.this, verify, Toast.LENGTH_SHORT).show();
dialog_mi(number_miss);setimg(url);checkverify(verify,player,Img_check);putfloat(Isso,Tx,Three);
//miss(number_miss);
//getballs(balls);
//checkplayer(player);
//checkvefify(verify,player);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(PlayActivity.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
@Override
public void onBackPressed() {
super.onBackPressed();
mDatabase.removeEventListener(listener);
Intent intent = new Intent(getApplicationContext(), MissionActivity.class);
intent.putExtra("level", mission);
startActivity(intent);
}