0

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?

Alexander
  • 107
  • 1
  • 2

2 Answers2

1

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):
1

This is happening because the database reference holds a strong reference to its value event listener. So even though your activity gets destroyed, you will still be able to receive messages through your value event listener if you don't remove it from your refState object.

Christilyn Arjona
  • 2,173
  • 3
  • 13
  • 20