Listing 3.7 savedInstanceState.getInt(...) Second Parameter


#1

Listing 3.7

...
if (savedInstanceSate != null) {
    [b]mCurrentIndex = savedInstanceState.getInt(KEY_INDEX, 0);[/b]
}

updateQuestion();

}

I don’t understand the meaning of second parameter ‘0’ in savedInstanceState.getInt(…) method.
Can I put another number(1, 2, 100, …) instead of 0 ?


#2

public int getInt (String key, int defaultValue)
Added in API level 1
Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Parameters
key a String
defaultValue Value to return if key does not exist

The second value is a default value. If you put ‘2’ instead of ‘0’ and for some reason KEY_INDEX had not been mapped then that function would make mCurrentIndex = 2 and the app would show question 3


#3

[quote=“wysoskyj”]public int getInt (String key, int defaultValue)
Added in API level 1
Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Parameters
key a String
defaultValue Value to return if key does not exist

The second value is a default value. If you put ‘2’ instead of ‘0’ and for some reason KEY_INDEX had not been mapped then that function would make mCurrentIndex = 2 and the app would show question 3[/quote]

Thank you! :smiley:


#4

[quote=“wysoskyj”]public int getInt (String key, int defaultValue)
Added in API level 1
Returns the value associated with the given key, or defaultValue if no mapping of the desired type exists for the given key.

Parameters
key a String
defaultValue Value to return if key does not exist

The second value is a default value. If you put ‘2’ instead of ‘0’ and for some reason KEY_INDEX had not been mapped then that function would make mCurrentIndex = 2 and the app would show question 3[/quote]

Thank you


#5

I was doing the challenge in chapter 5 and was wondering the same thing, thanks.


#6

Can someone please elaborate more. what does savedInstanceState.putInt(KEY_INDEX, mCurrentIndex); do?


#7

Hey Ashesh, I believe the putInt(KEY,Value) method will put a Integer Value into the Bundle Object’s KEY. Just a guess but I think I’m right. Android creates a Bundle Obj but it’s just a empty (I don’t think it’s truly empty at the start - someone confirm) structure of a Map. You find your way around the map using Key/Values pairs. But like an Array you gotta put values into the data Structure so you can use it, which is where that method putInt(Key,Value) comes into play.

[quote]From the book:
A Bundle is a structure that maps string keys to values of certain limited types[/quote]

So savedInstanceState is just a Map that holds Values like ints, strings, other primitive data types, and serialized classes. To find the value you use a Key. In the QuizActivity the “KEY_INDEX” is the KEY (or you could say its Apartment Address) that’s ‘mapped’ (or you could say that Holds) to ‘mCurrentIndex’ the value.


#8

Thanks a lot! You confirmed my understanding(intuition might be the right word for a newbie). I’m still moving ahead slowly n now in chapter 15. Takes a while to grasp some concepts but you do realize it with persistence I guess :slight_smile:. Damn those challenges r tough :angry: