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 trialHenry Kritikos
Courses Plus Student 1,070 PointsI'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.
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
56,414 PointsThe 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
16,570 PointsCarl Sergile
16,570 PointsThanks 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
56,414 PointsSeth Kroger
56,414 PointsThe 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.