Search code examples
androidfirebase-realtime-databaseandroid-activitylistener

Firebase listeners still listening after activity being finished


I have a Firebase Realtime Databse listener in a part of my code, which is:

DatabaseReference refState= FirebaseDatabase.getInstance().getReference("/Rooms/"+roomName+"/gameState");
refState.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    String gameState = dataSnapshot.getValue(String.class);
                    if (gameState.equals("choose_letter")) {
                        System.out.println("Starting game");
                        startGame();
                    }
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    System.out.println("Error");
                }

            }); 

Where startGame() function is:

private void startGame(){
         Intent i = new Intent(Salon.this, Game.class);
         i.putExtra("username",username);
         i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK); 
         startActivity(i);
          finish();      
    }

The code works and does what I want: when the "gameState" value in the datdabased is changed to "choose_letter", it starts a new activity called "Game" from the current activity "Salon" and finishes all underlying activities (including salon). During the game the "gameState" is changed several times, however when is set to: "choose_letter" the activity is reopened and in the console I can read "Starting game". So in conclusion, somehow, even with the activity "salon" closed, the Database Listener declared in that activity is still working. I would like to know, how is that possible and how can I fix it?


Solution

  • Before going to next Activity remove the value event listener

    refState.removeEventListener(listenerName)
    

    Try this code

    Inside onCreate

    DatabaseReference refState = FirebaseDatabase.getInstance().
        getReference("/Rooms/"+roomName+"/gameState");
    refState.addValueEventListener(stateValueEventListner);
    

    Outside onCreate

    private ValueEventListener stateValueEventListner = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
               try {
                    String gameState = dataSnapshot.getValue(String.class);
                    if (gameState.equals("choose_letter")) {
                        System.out.println("Starting game");
                        startGame();
                    }
                  }
                  catch(Exception e)
                  {}
                }
    
                @Override
                public void onCancelled(DatabaseError databaseError) {
                    System.out.println("Error");
                }
    
            }); 
    

    Remove value event listener

    refState.removeEventListener(stateValueEventListner):