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 Getting Data from an Intent

So...what's going on?

Hello,

I have a question regarding 'intent.getStringExtra()'. We added "name" as the 1st parameter, are we referring to the "name" in 'MainActivity'. Also, why do we need to create 2 'intent' classes?

Please Help!

1 Answer

Kevin Faust
Kevin Faust
15,353 Points

Hi,

We only created the actual intent object once in our MainActivity. Since we want to pass data from one activity to another, we used the "putExtra". the first parameter is a key and the second parameter is the data we want to pass. the key can be any string which we can use to access the data from the other activity.

In the other activity, we can only have gotten there via intent. and as we know, we are passing data from one activity to another. so we first call "getIntent" to get the intent object we passed (not creating a new intent) and then we call "getStringExtra" on that intent and pass in our key as a parameter to access the actual data (the name string)

you are right about "name" referring to the "name" key in main activity

Thank you!