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


How 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

You 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");

//Your second class
String awesomeGuy = getIntent().getStringExtra("Awesome Treehouse Guy");

thanks, it worked

Ben Jakuben
Ben Jakuben
Treehouse Teacher

This is covered in the video Easy Sharing with Intents, if you're interested.