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

Android new intent

When a user presses a button (there are multiple buttons) I want the program to open a new intent or activity with the string parsed on the current activity to the new activity also I want to handle the data that is being parsed once a button is pressed with a switch statement for example:

case button1: send foo to new activity case button2: send bar to new activity

etc

1 Answer

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

We haven't talked about this too much in our projects here, but Activities pass information between each other by attaching "extras" to the Intents. You can attach simple or more complex data. For a String, check out this Intent putExtra() method:

http://developer.android.com/reference/android/content/Intent.html#putExtra(java.lang.String, java.lang.String)

Quick example:

Intent intent = new Intent(this, TargetActivity.class);
intent.putExtra("sharedString", "This is the String to share");
startActivity(intent); 

And then obviously from within our new activity we can retrieve this information by getting the extra that was passed in and looking for the key specified (In Ben's quick example above this would be "sharedString")

String myAmazingInformation = getIntent().getStringExtra("sharedString");
Ben Jakuben
Ben Jakuben
Treehouse Teacher

Excellent - thanks for rounding out the example! As a side note, for more complex information you can attach a Bundle object to an Intent.