2

I have the the following code:

Intent intent = new Intent(MainActivity.this, NextActivity.class);
startActivity(intent);

I can't understand why do we put the keyword this with the current class and the keyword class with the name of the class we wish our intent to start.

What I know is:

this refers to the current instance of the class and class is a keyword we use to define new classes. Can somebody explain me what are we trying to say by using this and class with the name of classes?

Trinimon
  • 13,839
  • 9
  • 44
  • 60
Arslan Ali
  • 17,418
  • 8
  • 58
  • 76

3 Answers3

3

From the documentation:

public Intent (Context packageContext, Class cls) Added in API level 1

Create an intent for a specific component. All other fields (action, data, type, class) are null, though they can be modified later with explicit calls. This provides a convenient way to create an intent that is intended to execute a hard-coded class name, rather than relying on the system to find an appropriate class for you; see setComponent(ComponentName) for more information on the repercussions of this.

Parameters

packageContext A Context of the application package implementing this class.

cls The component class that is to be used for the intent.

In simpler words: your constructor needs a Context and a Class. A Class refers to the Java class files that are created for a given Java source file, and it might be something currently not in use. Contexts is completely unrelated to Classes, as far as usage does, and when passed to a method, generally refer to something that has already been instanced.

MowDownJoe
  • 728
  • 1
  • 11
  • 29
  • So we use the keyword **class** to tell android that what we are passing is of type **class**. If you say 'yes', Why don't we do this with any other type? For example, I pass an integer value to a function, I don't tell andoird, "Hey, Andorid this was an int, don't misunderstand it." Why in the case with **class** only? – Arslan Ali Apr 03 '13 at 20:08
  • 1
    Because if you don't use the keyword class, the system will assume you're passing an [`Object`](http://developer.android.com/reference/java/lang/Object.html) of some sort named MainActivity (using your example), instead of its Java class file, and then the compiler will complain that there isn't one. – MowDownJoe Apr 03 '13 at 20:38
1

To make a new intent all you need to say is

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

This is defining the current class, whereas you wouldn't be able to use this for the next activity as it isn't the current class. You would use class for that.

Ross Hunter
  • 58
  • 10
  • 1
    I was using my code inside of an anonymous event click listener. Perhaps, therefore using the name of the class with the keyword **this** was a requirement. You can guide me! – Arslan Ali Apr 03 '13 at 19:57
  • Well, you use .this when you have another variable in the same scope of the same name. So this could be the issue with yours. – Ross Hunter Apr 03 '13 at 20:22
0

this will also work fine, but when you are using dialog, then the keyword this doesn't refer to MainActivity. We use the keyword this to avoid those situations when we use MainActivity.this .If you are working in an Activity Class context, then using only this keyword works fine. Try it yourself and the reason for using the keyword class- I think the argument of Intent() method should be of class type, that;s why we pass class type argument to the function(constructor).

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
mathlearner
  • 7,509
  • 31
  • 126
  • 189