I am developing a simple Android game consisting of a MainActivity and a GameActivity. In the MainActivity the user can select some options and then start a game with these options/variables (i.e. game mode, game difficulty, etc.)
Intent gameIntent = new Intent(this, GameActivity.class);
gameIntent.putExtra("gameMode", 3);
startActivity(gameIntent);
Since I have quite a few variables to store (26 to be precise) and since Java is object oriented I thought about using my own Game class to store and retrieve all variables like this:
public class Game implements Parcelable {
private int gameMode;
private int gameDifficulty;
// Constructor
public Game(int gameMode, int gameDifficulty) {
this.gameMode = gameMode;
this.gameDifficulty = gameDifficulty;
}
// getters and setters here
}
and put this as a Parcelable into my intents:
Game myGame = new Game(gameMode, gameDifficulty);
Intent gameIntent = new Intent(this, GameActivity.class);
gameIntent.putExtra("game", myGame);
startActivity(gameIntent);
My question is: Is using an Object to store and pass game variables convenient or is it over the top? What's the best approach? Have I missed something? Thank you in advance!
Of course saving setting in the object is good approach. If you have more than one activity that depends on that object then you should consider to cache it using singleton, and save it to internal storage. If you have one activity that depends on settings (like in your case I guess) it is ok to use class that implements Parcelable and pass the instance of that class through Intent to GameActivity.