I have an activity to play a game. The user may be resuming from a saved state or initiating a new game. If resuming, the identifier for the game to resume is passed to the activity in the bundle. If it is a new game, that part of the bundle isn't passed, and is therefore null
. I currently implement this as follows...
Bundle bundle = this.getIntent().getExtras();
int GameNumberToResume;
boolean StartNewGame = false;
try
{
GameNumberToResume = bundle.getInt("GameToResume");
}
catch (Exception e)
{
System.out.println("Exception Catch, so we start a new game.");
StartNewGame = true;
System.out.println((e.toString()));
}
...and it works. StartNewGame
drives the decision tree on if we are starting a new one or resuming a saved one, and if we are resuming, GameNumberToResume
has the game ID to resume. However, Android Studio throws the soft warning...
Variable 'GameNumberToResume' is assigned but never accessed.
...because in the parts of the decision tree when I need the value of the game to resume, I pull it straight from the bundle via bundle.getInt("GameToResume")
.
So here's my question: What should I do different? I can make the warning go away by referencing the variable GameNumberToResume
downstream instead of pulling it from the bundle, but it doesn't seem to me like that would change anything. The intent of the warning is to point out that I'm wasting memory, and if I do this, I still have two things in scope that both contain the same value.
- Is there a way to detect the absence or presence of "GameToResume" in the bundle without doing a variable assignment in a try/catch loop?
- If I move the declaration inside the
try
part of the loop, then fire off aSystem.gc();
after thecatch
portion of the loop, would it free up what was used by the variableGameNumberToResume
?
I know in this specific case it probably doesn't matter, but it is simple enough to follow and illustrates a general hole in my understanding of how to efficiently code Android.