removeEventListener not removing listener in firebase

前端 未结 4 1419
隐瞒了意图╮
隐瞒了意图╮ 2021-02-19 10:40

I want to remove addValueEventListener listener from a firebase ref when value of particular field is true.

ValueEventListener valueListener=null;

private void          


        
4条回答
  •  眼角桃花
    2021-02-19 11:07

    I hope this will help you... just yo can use this method, it's work for me...

    private DatabaseReference dbRef;
    

    Write from onCreate()

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        dbRef= database.getReference("YOUR_PATH").child("KEY_NAME");
        InitListener();
        dbRef.addValueEventListener(valueEventListener); // ADDIND_LISTENER
    

    Write from onDestroy()

    if (dbRef!=null){
            dbRef.removeEventListener(valueEventListener);
            valueEventListener=null;
            dbRef=null;
    
        }
    

    Listener

     private void InitListener(String templeId) {
        valueEventListener=new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
    
    
            }
    
            @Override
            public void onCancelled(DatabaseError databaseError) {
    
            }
        };
    

    }

提交回复
热议问题