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

Carney Bernard
Carney Bernard
3,615 Points

Still Cannot Figure Challenge Out

I still can't get this challenge right. Very frustrating :( can someone please help me

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

// mFuelLevel is set elsewhere. // Code ommitted for brevity! int mFuelLevel= 0; mLaunchButton = (Button)findViewById(R.id.launchButton); mLaunchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { // Add your code in here! Intent intent = new Intent(LaunchActivity.this, FlightActivity.class); intent.putExtra("Fuel_Level", "mFuelLevel"); startActivity(intent);

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

public class LaunchActivity extends Activity {

  public Button mLaunchButton;
  public int mFuelLevel;

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

    // mFuelLevel is set elsewhere.
    // Code ommitted for brevity!
      int mFuelLevel= 0;
    mLaunchButton = (Button)findViewById(R.id.launchButton);
    mLaunchButton.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        // Add your code in here!
        Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);
        intent.putExtra("Fuel_Level", "mFuelLevel");
        startActivity(intent);
      }
    });
  }
}

5 Answers

Stone Preston
Stone Preston
42,016 Points

the task says the key needs to be "FUEL_LEVEL". not "Fuel_Level". also, you are currently passing the string "mFuelLevel", however you need to pass the value of the mFuelLevel variable itself.

Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);
// Use the correct key and pass in mFuelLevel, not "mFuelLevel"
intent.putExtra("FUEL_LEVEL", mFuelLevel);
startActivity(intent);
Vincent Rickey
Vincent Rickey
5,581 Points

Hey Stone, quick question-- in the first 15 seconds of the "Sending Data to a New Activity" video, Ben states that it is not a good idea to pass class member variables from one activity into another for scalability reasons. In the video, Ben declared a new name variable in the startStory method. Why are do we pass a member variable into the intent in this code challenge?

Just copy and past

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);
              intent.putExtra("FUEL_LEVEL", fuelLevel);
            startActivity(intent);
        }
    });
}

}

intent.putExtra("Fuel_Level", FuelLevel);

The is found your in heart. You must learn to love again and maybe shine a light on your dark soul. Kiddo

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);
        }
    });
}

}