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!
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
meng chen
378 PointsHow to pass variable between activities in Android
I'm trying pass a string variable back to main activity as the text for a button. Anyone has any idea how to do so?
1 Answer

Ernest Grzybowski
Treehouse Project ReviewerYou can use an intent extra. You can read more about intents here: http://developer.android.com/reference/android/content/Intent.html
Off the top of my head, when you create an intent you would want something like this.
//Your first class
Intent myIntent = new Intent (this, NextClass.java);
theintent.putExtra("Awesome Treehouse Guy", "Ernest");
startActivity(myIntent);
//Your second class
String awesomeGuy = getIntent().getStringExtra("Awesome Treehouse Guy");
meng chen
378 Pointsmeng chen
378 Pointsthanks, it worked
Ben Jakuben
Treehouse TeacherBen Jakuben
Treehouse TeacherThis is covered in the video Easy Sharing with Intents, if you're interested.