Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Android Build an Interactive Story App (Retired) Intents and Multiple Activities Getting Data from an Intent

I'd like to use the getIntExtra method but am unsure how to use it.

I haven't used this method before. Just not sure how to code it.

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();
    String mFuelLevel = intent.getIntExtra(FUEL_LEVEL, int defaultValue);

  }
}

1 Answer

Seth Kroger
Seth Kroger
56,406 Points

The first argument is a String for the name or key for the value you gave when the putting the extra into the intent. According to the code challenge it should be "FUEL_LEVEL" with the quotemarks. The second argument is an int number or variable for the default value, which the challenge states should be -1. So the code should look like:

mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);
Carl Sergile
Carl Sergile
16,570 Points

Thanks for this answer, but I have a question.... In the video. the value type String was used in the name variable(String name =....) Why didnt you do?: int mFuelLevel = intent.getIntExtra("FUEL_LEVEL", -1);

Was because mFuelLevel is already mentioned or what?

Seth Kroger
Seth Kroger
56,406 Points

The variable begins with an 'm' prefix which means it's a member variable that's declared further up in the code near the start. It's already declared as type int. In this challenge the extra is passing a number value, not a string value like in the prior video.