1

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 a System.gc(); after the catch portion of the loop, would it free up what was used by the variable GameNumberToResume ?

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.

LDMJoe
  • 1,591
  • 13
  • 17
  • 1
    You can use the `Bundle#containsKey()` method, or use the two-parameter `Bundle#getInt()` method, with the second argument being a default value - e.g., `-1` - that you can test for after the call. There's also the `Intent#hasExtra()` method. – Mike M. Mar 28 '16 at 01:48
  • http://developer.android.com/reference/android/os/BaseBundle.html#containsKey(java.lang.String) – Eugen Pechanec Mar 28 '16 at 01:52
  • tips: variable name shouldn't use capital letters. At first I thought GameNumberToResume is a Class – aldok Mar 28 '16 at 02:35

3 Answers3

4

Checkout

bundle.containsKey ("GameToResume");

it will return whether this key is there or not in bundle.

UMESH0492
  • 1,701
  • 18
  • 31
1

Do not use the try-catch here. And you can initialise the game number like int GameNumberToResume=Integer.MAX_VALUE. Then remove the try-catch. Just use if(xx==Integer.MAX_VALUE).
In my opinion, try-catch can only use for the unknown error or unpredictable situation. Here, you know everything.

blackdog
  • 2,007
  • 3
  • 25
  • 43
1

You can utilize getInt second parameter to put default value, like this.

Bundle bundle = this.getIntent().getExtras();

// Don't forget NullPointerException
if(bundle != null){
    final int IMPOSSIBLE_VALUE = -1; // default value
    boolean startNewGame = false;

    int gameNumberToResume = bundle.getInt("GameToResume", IMPOSSIBLE_VALUE);
    if(gameNumberToResume != IMPOSSIBLE_VALUE){
        startNewGame = true;
        // continue ..
    }    
}
aldok
  • 17,295
  • 5
  • 53
  • 64