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

Moussa Ismail
Moussa Ismail
4,175 Points

Having trouble with Intent Documentation

I have tried a number of the different intent documentations given and have hit a brick wall trying to solve this challenge ... I feel like I'm missing something here.

Unlike the examples in the video which dealt almost strictly with Strings, this challenge is using a String and an Integer and now I'm confused.

FlightActivity.java
import android.os.Bundle;

public class FlightActivity extends Activity {

  public int mFuelLevel = -1;

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

    Intent intent = getIntent();
    String mFuelLevel = String.getIntExtra("FUEL_LEVEL", -1);
  }
}

1 Answer

Ken Alger
STAFF
Ken Alger
Treehouse Teacher

Moussa;

Task 2

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.

mFuelLevel has already been declared outside of onCreate, so we don't need to redo that here, therefore we need:

mFuelLevel = something as our answer. We need to set it to a value from the Intent, so

mFuelLevel = intent.something

seems like what we need. Now, when mFuelLevel was defined it was defined as an int. In looking at the link provided, there is a method called getIntExtra which takes a string and then an int as the default value. That sounds like it would work for us, and it looks like that is what you used as well. Nice work!

If we combine all of that info together, we wind up with

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

as the code we need to complete Task 2.

Does that make any sense?

Ken

Moussa Ismail
Moussa Ismail
4,175 Points

Awesome, it works! Thanks Ken, I now see what you did there