Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android

Set value in activity based on button click from another

Hi, Here are what I'm trying to do: there are three buttons that leads to the same second activity. However, depending on which button is clicked, a value in the second activity will be set differently. I'm wondering what is the most efficient way of doing so. thanks

3 Answers

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

You will want to add a piece of data to the Intent you use when you tap on the button. Ernest Grzybowski just posted some helpful code in this other forum post about a similar issue. You can also find more details about passing data with Intents in the Easy Sharing with Intents video.

Now what data should you pass? You could pass a simple String or number to indicate which button. I would recommend making a publicly available constant in your first Activity that you could reference in the second Activity. That way you could make certain you are referencing the same keys and values. Here's a quick example:

FirstActivity.java

public static final String BUTTON_KEY = "BUTTON_KEY";
public static final String BUTTON_VALUE = "first_button";
...
intent.putExtra(BUTTON_KEY, BUTTON_VALUE);

SecondActivity.java

String buttonValue = bundle.getString(FirstActivity.BUTTON_KEY);
if (buttonValue.equals(FirstActivity.BUTTON_VALUE)) {
    // do something for the 1st button...
}

I have 3 activities structured like such: A > B > C A is the main screen B is the profile list with a button "add profile" C is the page for enter profile information

putExtra was used in C to pass information to B for display, and it seems to work properly. However, when I click "Back" in B to A, and go back to B. No information were displayed, if I enter C and go back to B again, the information entered will come back. I wonder why is it behaving this way

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

A few brief points (maybe I or someone else can add more detail later, but I'm short on time!):

Check out startActivityForResult. If you start C "for result" then you can pass back the data to B as a result of what was done on C.

How is your data in B being stored/loaded? It sounds like you're losing data because of the Activity lifecycle. When B is paused (the onPause() event), you should store the data in something like SharedPreferences and then fill it back in in the onResume() method. That's one thing you could try, at least.