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 Starting a New Activity

Jonathan Grieve
MOD
Jonathan Grieve
Treehouse Moderator 91,253 Points

What'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 :)

LaunchActivity.java
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
Aaron Arkie
5,345 Points

Hey 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
Aaron Arkie
5,345 Points

I 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.

public class MainActivity extends AppCompatActivity
{
    private void startStory()
    {
        Intent intent = new Intent(this, StoryActivity.class); // "this" will refer to the MainActivity object
        startActivity(intent);
    }
}

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.

mLaunchButton.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // In the scope of the anonymous inner-class "View"
        Intent intent = new Intent(LaunchActivity.this, FlightActivity.class); 
      }
    });
Jonathan Grieve
Jonathan Grieve
Treehouse Moderator 91,253 Points

So we're being tested on the difference between using an intent in different scopes. :)