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
Carney Bernard
3,615 PointsConfused with intents and current challenge
Hi, I have been trying to figure this out for a little while now but I can't seem to grasp my mind around intents and what they are used for. All of this is making me feel so dumb! Obviously I watched the tutorial but I need an explanation in a simpler form. Also I am struggling with the current challenge on intents:
"We've updated our LaunchActivity to include a fuel level(mFuelLevel) for the Spacecraft. This value needs to be passed to the FlightActivity. Add the code to pass this value along via Intent. Use the key "Fuel_Level".
Here is my code! Please help!!!!!!!
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_launch);
// mFuelLevel is set elsewhere.
// Code ommitted for brevity!
mLaunchButton = (Button)findViewById(R.id.launchButton);
mLaunchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v, mFuelLevel) {
// Add your code in here!
Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);
intent.getStringExtra("Fuel_Level", FlightActivity);
startActivity(intent);
}
});
} }
1 Answer
Mikael Enarsson
7,056 PointsAs far as I can understand, an Intent is essentially a way to handle execution of and communication between Activities (and other things, but Activities are what I have handled thus far, and as such the only things I feel somewhat comfortable to offer an opinion on).
More reading can be done in the documentation, which seem fascinating, and I'll get back to them when I get the time.
I hope this is of some help ^^
Carney Bernard
3,615 PointsCarney Bernard
3,615 PointsThank you for the link! Could you give me any guidance regarding the code above please? That would be much appreciated!
Mikael Enarsson
7,056 PointsMikael Enarsson
7,056 PointsAh, yes, of course! I'm sorry I missed that (I blame being tired XD).
Anyway, there are two issues with your code:
First, they are asking you to add the extra to the intent, not get it from it. You would use
.putExtra(), and the same syntax, so that should be fine ^^The other issue is a bit silly, but
FUEL_LEVELis supposed to be all caps.Once again, sorry about that, and hope that this helps ^^