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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With