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

Intent Methods

Hello. I used this method: mFuelLevel = intent.getIntExtra("FUEL_LEVEL", 0);

What did I do wrong?

With respect, Aleksandr.

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 = new Intent();
    mFuelLevel = intent.getIntExtra("FUEL_LEVEL", 0);


  }
}

You won't be able to get any extras from your intent because you just declared it as a new Intent on the line above. What are you trying to accomplish? Are you trying to create an intent to open a new Activity or possibly broadcast out for broadcast receivers to pick up? Or are you trying to store the fuel level out to disk and then retrieve it when FlightActivity is created?

2 Answers

You need to get the intent that was used to start the current activity. i.e.

// instead of this . . 
   Intent intent = new Intent();
// do this . . 
  Intent intent = getIntent();

Also, make sure you are passing the correct default value to getIntExtra().

Thank you. I just missclicked.

So, I did not answer your question?

No, everything is OK. Thanks for help.