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

I didn't understand question?

What's wrong in this and what is default value?
import android.os.Bundle;

public class FlightActivity extends AppCompatActivity {

    public int fuelLevel = 0;

    protected void onCreate(Bundle savedInstanceState) {

        // Add your code below!
      Intent intent =getIntent();
      String intent = getStringExtra("FUEL_LEVEL");


2 Answers

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Let's start by removing this line from your code:

String intent = getStringExtra("FUEL_LEVEL");

That is trying to create a new variable of type String with the name intent, but that's already being used by your Intent variable.

Next, this question asks:

Now set fuelLevel to the value from the Intent. Check the Intent documentation if you need help finding the correct method to use. Use "FUEL_LEVEL" as the 'name' and -1 as the 'defaultValue'.

That means your next line needs to set the fuelLevel variable that's already declared as a class member variable on line 5. To set this variable, you need to follow the link to find the appropriate Intent method that can be used to load an int variable (as opposed to a String variable, which we've seen in the videos).

When you find the right method, you'll notice that it has a 2nd parameter that is to be used as the default value, in case what you're trying to get out of the intent isn't in there. That's where the -1 in the instructions comes into play.

Hope this helps!

Hi there,

You've got the first part of the challenge nailed - good work!

Next, you want to set the value of the existing fuelLevel variable - it is set up to hold an int. You want to find the right method in the documentation that gets an Int extra that's been passed across the activities. You've tried getStringExtra - try getIntExtra, perhaps?

You call that method on the intent you have set up - use dot notation. Then this method takes two parameters. First is the key to the key/value pair you want to access. Second is a default value in case this fails. That default value is -1. The key, we've been told is "FUEL_LEVEL". So, pass in two parameters, the first is the string and the second is a number (so no ""). Assign the result of that to fuelLevel.

That all looks like:

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

I hope that helps.


Ben Jakuben
Ben Jakuben
Treehouse Teacher

High five for the overlapping help! :)

Agreed! :hand:

:+1: :smile: