In my application, I have an entity called Game that has, among other attributes, an ID . I need every time this entity is instantiated the ID is incremented by +1. This works until the application is terminated, because when I open the app again the ID count is restarted, so when I create a new Game and submit it to Firebase, it overwrites the element that already existed with that ID instead of creating a new one. I need to save some of this IDs count, or to regain the size of the Games list already registered in Firebase so that when the application starts again ID keep counting where you left off. What is the best way to do this? And how to do it?
Here's how I'm doing to count the ID in the Game Entity:
public class Game {
private static int cont = 1;
private int id;
...
//Construtor
public Game() {
this.id = cont++;
}
}
And here's where I'm submitting the new Game to Firebase at RegisterGamesActivity2:
private boolean registerGame(Game game){
try{
firebase = FirebaseConfig.getFirebase().child("Game");
firebase.child(String.valueOf(game.getId())).setValue(game);
Toast.makeText(RegisterGamesActivity2.this, "Jogo cadastrado com sucesso!", Toast.LENGTH_LONG).show();
return true;
}
catch (Exception e){
e.printStackTrace();
return false;
}
}