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

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.