Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android Build an Interactive Story App (Retired) Intents and Multiple Activities Introducing String Resources

hakan Gülay
hakan Gülay
8,489 Points

I am confused about this code

intent.putExtra(getString(R.string.key_name),name);

İn this code , when user enters something it is going to name then that value is going to R.string.key_name right ? and then in other class we can get that value like this

Intent intent = getIntent(); String name = intent.getStringExtra(getString(R.string.key_name));

am ı correct or not ? and one more question is what getString is doing here ?(in first code)

2 Answers

Daniel Hartin
Daniel Hartin
18,106 Points

Hi Hakan

So the line

intent.putExtra(getString(R.string.key_name),name);

is adding a keyvalue pair to the intent, what I mean by this is the value (this is the real data we want) is added as the variable name and the method getString(R.string.key_name) returns a string constant which is defined inside your Strings.xml file to act as a key for this data.

Keyvalue pairs allow data to be stored in a way that can be accessed similar to a dictionary i.e. you use a value to look up the data or definition you want. The reason we use a constant value is because this key must match exactly the key you use to return the data and by defining it separately inside the strings xml file it cuts down the probability of a typo.

So to answer your question directly no you are not assigning the value of name to R.string.key_name you are simply defining the link between them so you can retrieve the data inside the variable name inside another activity.

I hope this makes sense

Daniel

hakan Gülay
hakan Gülay
8,489 Points

Thank you so much Daniel! I got it now :)