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 trialSeye Kuyinu
2,581 PointsConfused about LoginActivity.this and 'this'
In the two classes for the intent in MainActivity.java and LoginActivity.java, Ben used "this" and "LoginActivity.this" respectively for the first parameter of the new Intent() declaration. Why are these different and not just 'this' for the two?
4 Answers
Khaled Beddakhe
13,960 Points"this" doesn't refer to the Activity Class object (LoginActivity object) so ..the compiler will get confused.. since you call it from inside onclickListener , "this" will refer to the onClickListener object and here you need to precise the object you want to use "LoginActivity.this".. just you have to pay attention to the scope.
Seye Kuyinu
2,581 PointsBeddakhe Khaled Thanks so much
Seye Kuyinu
2,581 Pointsoh!!!!! I think I get it now Beddakhe Khaled. Is there any other way I could have referenced the activity without saying 'this'? Like, could I have called out the name of the activity directly? Thanks so much for this!!
Khaled Beddakhe
13,960 Pointsactually you have to precise the object you want to use which is an instance of your Activity class and not the Activity class itself, and the best and the easiest way to get the current object in java is to use the keyword "this". sometimes you have to use the getActivity() if you are inside a fragment or something like that
you will understand everything better in treehouse courses I wish you good luck with it