Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between this and Activity.this

For example

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

eclipse error: The method setClass(Context, Class) in the type Intent is not applicable for the arguments (FirstActivity.ClickEvent, Class)

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

But that will be correct. Anybody can explain the difference between those two ? Thanks.

like image 331
user1325996 Avatar asked Apr 11 '12 08:04

user1325996


People also ask

What is activity this in Android?

An activity provides the window in which the app draws its UI. This window typically fills the screen, but may be smaller than the screen and float on top of other windows. Generally, one activity implements one screen in an app.

What is the difference between main activity and main activity Java?

This also refers to the context of current Class. MainActivity is just a class name that extends Activity or Activity instance. MainActivity is class name and this is a keyword referring to particular context.

Is this and this Activity is same?

this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to. Activity. this points to the instance of the Activity you are currently in.


2 Answers

this refers to your current object. In your case you must have implemented the intent in an inner class ClickEvent, and thats what it points to.

Activity.this points to the instance of the Activity you are currently in.

like image 80
Shubhayu Avatar answered Oct 06 '22 00:10

Shubhayu


Shubhayu's answer is correct, but I just want to make clear for anyone who see this question that this and Activity.this is the same if you are using it directly in the activity.

This is answered here

Example:

@Override protected void onResume() {     super.onResume();      Log.d("Test", this.toString());     Log.d("Test", MainActivity.this.toString()); } 

Result:

D/Test: com.example.app.MainActivity@e923587 D/Test: com.example.app.MainActivity@e923587 
like image 22
Samuel Avatar answered Oct 05 '22 23:10

Samuel