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

I not understand what I need to do mFuelLevel to pass the int extra into a string like in the video

In the video the member variable was already a string so he just use the intent get string but in the exercise the member variable is an int so when I try to follow what Ben did it did not work . I also try get int but that did not work can someone show me the proper code and explain it to me Here is the task: 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!
    Intent intent = getIntent();
    mFuelLevel= intent.getStringExtra("FUEL_LEVEL");
  }
}

1 Answer

Harry James
Harry James
14,780 Points

Hey there Amon!

You got the first bit right with the getIntent() but, the next bit was a bit trickier!

What you want to do is use the method getIntExtra() instead because the FUEL_LEVEL value was an integer! Take a look at the documentation here. I know, you may hate me for linking a documentation link (I feel the same sometimes) but, it actually explains it quite well here.


So, the method takes in two parameters - name of the item you want to extra from. This should be provided as a string...

And a defaultValue as an integer (Don't use quotes this time!). This is the value that will be returned by the method if the FUEL_LEVEL value was not passed in to the method for some reason.


Hopefully this makes sense to you! If there's something you don't understand still, give me a shout and I'll try my best to explain it :)

Thanks I follow the link was very helpful