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 Intents and Multiple Activities Putting Data in an Intent

prosper nyamukondiwa
prosper nyamukondiwa
656 Points

intent

We've updated our LaunchActivity to include a fuel level (fuelLevel) for the spacecraft. This value needs to be passed to the FlightActivity. Add the code to pass this value along via the Intent. Use the key "FUEL_LEVEL"

LaunchActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class LaunchActivity extends AppCompatActivity {

    public Button launchButton;
    public int fuelLevel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_launch);

        // fuelLevel is set elsewhere.
        // Code ommitted for brevity!

        launchButton = (Button)findViewById(R.id.launchButton);
        launchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // Add your code in here!
                Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);
                startActivity(intent);



            } 
        });
    }
}

1 Answer

Hey Pro; You need to add the line intent.putExtra("FUEL_LEVEL", fuelLevel); so that the value can be passed.

Happy coding!!

Jasper Allijn
Jasper Allijn
2,743 Points

I don't understand this at all. Where does the "FUEL_LEVEL" all of a sudden come from? I know the challenge told me to write that, but doesn't it need to reference something in the code for it to work? Or can I just make up words and it's all fine and dandy?

I don't think I really understand the concept of passing a value through intent yet, but the answer seems really odd to me and (with my current understanding, or lack thereof) makes more sense if it was:

intent.putExtra("fuelLevel", FlightActivity); instead of intent.putExtra("FUEL_LEVEL", fuelLevel);

So passing "fuelLevel" -> FlightActivity as explained in the challenge ("This value needs to be passed to the FlightActivity.") I've watched the video multiple times and googled it, yet I keep falling back to the same conclusion. What key point am I missing?