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 Getting Data from an Intent

We previously added a FUEL_LEVEL to our spacecraft. Now in FlightActivity, we want to get the value that we put in the

please help me with this ??

We previously added a FUEL_LEVEL to our spacecraft. Now in FlightActivity, we want to get the value that we put in the Intent. Start by declaring an Intent variable and getting the Intent used to start this Activity.

FlightActivity.java
import android.os.Bundle;

public class FlightActivity extends Activity {

  public int mFuelLevel = 0;

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

    // Add your code below!

  }
}

8 Answers

william parrish
william parrish
13,774 Points

Sorry I did this non my phone and I didn't go through the first time correctly. Try getIntExtra()

Thanks a lot

william parrish
william parrish
13,774 Points

They are asking you to do two things:

  1. Declare an Intent

  2. Assign it the value, of the intent used to start the activity.

Which one of those are you unsure about, and did you watch the video before the quiz?

the second one ??!!

the first one is Intent intent = getIntent();

in fact this is the what they asked >>>?

Now set mFuelLevel the the value from the Intent. Check the Intent documentation if you need help finding the correct method to use. Use "FUEL_LEVEL" as the key and -1 as the default value.

william parrish
william parrish
13,774 Points

There is a method called. Intent.putExtra(). Which takes a string for the key, and an int for a value. Use that, with the information you just referenced. Make sure to use your "intent" .

I put this code mFuelLevel = intent.putExtra(1);

what should I do ?!!

william parrish
william parrish
13,774 Points

Sorry, you want to use 'getExtra'. Not put. The int is already there. You just have to get it out by key.

I changed to

mFuelLevel = intent.getExtra("FUEL_LEVEL",1);

but it said that there is a different method to get an int extra ?!!

william parrish
william parrish
13,774 Points

It may require you to specify a default value. Add another integer into the parameters, you can use 0 or 1. Or whatever you want.

so can someone just put the answer to this task up because i have no idea \

Ammon D'Addabbo
Ammon D'Addabbo
7,866 Points

mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);

There is the answer:

Intent intent = getIntent(); fuelLevel = intent.getIntExtra("FUEL_LEVEL", 1);

Patrick you need to use the exact value used in the question i.e -1