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 trialJonathan Grieve
Treehouse Moderator 91,253 PointsWhat's wrong with using only "this" as context?
Hi all,
So I'm catching up with what I've learned about Android programming so far. I understand how to launch a basic intent but there's one thing that confuses me.
In the interactive story app, we're able to use (this) as the first context parameter of our intent. But the challenge requires us to use LaunchActivity.this as the parameter?
What's the difference between the 2?
Thanks :)
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class LaunchActivity extends Activity {
public Button mLaunchButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launch);
mLaunchButton = (Button)findViewById(R.id.launchButton);
mLaunchButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Add your code in here!
Intent intent = new Intent(LaunchActivity.this, FlightActivity.class);
}
});
}
}
1 Answer
Aaron Arkie
5,345 PointsHey Jonathan,
I believe the instructor explained it in the video though I am not sure of the time frame. Using "this" by itself refers to the current calling object in which one of its functions is being used. In the context of this challenge question we had to pass in LaunchActivity.this as an arguement for the fact that we are in an anonymous inner-class. If we were to simply pass in the "this" keyword we would be referring to the anonymous-inner class instead of the LaunchActivity object which I believe is due to scope. Let me know if this makes sense!
Aaron Arkie
5,345 PointsAaron Arkie
5,345 PointsI just got to the part that you were on and I just wanted to clarify a bit more. In the video the intent was created in a method inside the MainActivity class body so this method has direct access to the calling object.
In the above challenge problem the Intent object was created in an anonymous-inner class so the "this" keyword will refer to the calling object which is in the scope of the calling "View" object. By using LaunchActivity.this we are referring to the calling object LaunchActivity instead.
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsSo we're being tested on the difference between using an intent in different scopes. :)