1
Intent intent = new Intent(this, SecondActivity.class);
    startActivity(intent);

I'm talking about that line of code above. Which I wrote in my MainActivity class under the onCreate method. It is supposed to activate the second activity. But I want to understand what is that "this" in the parameter?

Chris Mikkelsen
  • 3,987
  • 9
  • 29
  • 41
  • from the docs https://developer.android.com/reference/android/content/Intent.html#Intent%28android.content.Context,%20java.lang.Class%3C?%3E%29 – karan Jun 01 '16 at 08:50
  • Intent first param take Context class instance and in your case if MainActivity extending Activity then we can also pass `this` refers to a reference of the current class. because MainActivity class extending Activity and Activity is subclass of Context class. so we are able to pass `this` as first param to Intent constructor – ρяσѕρєя K Jun 01 '16 at 08:53

1 Answers1

1

You simply need to read the documentation associated with this call :

@param packageContext A Context of the application package implementing this class. @param cls The component class that is to be used for the intent.

So the first parameter is simply a context

If you try to dig deeper, you'll see that this context is used to create a ComponentName, which says

A Context for the package implementing the component, from which the actual package name will be retrieved.

Raghavendra
  • 2,305
  • 25
  • 30
NSimon
  • 5,212
  • 2
  • 22
  • 36
  • Which means if I will be using it inside a Fragment class it should be written as Intent intent = new Intent(getActivity().getApplicationContext(), SecondActivity.class); startActivity(intent); – Chris Mikkelsen Jun 01 '16 at 08:54
  • Or simply `Intent intent = new Intent(getContext(), SecondActivity.class);` – NSimon Jun 01 '16 at 08:55