The difference between Android this and Activity.this _android

Source: Internet
Author: User

There are two things that can happen when you write a statement:

Toast.maketext (alarmactivity.this, "Alarm clock cancellation", toast.length_short); 
 

In English in Google search what ' s difference between this and activity.this, finally have the result (in fact, they use Java in the back of the classname.this and this Before the search is activity.this, so there is no result, this should be flexible to improve their search ability.

In StackOverflow found the answer:

Http://stackoverflow.com/questions/10102151/whats-the-difference-between-this-and-activity-this

Intent Intent = new Intent (this, secondactivity.class); 
 
 Eclipse Error:the Method SetClass (context, Class) in the type Intent isn't applicable for the arguments (firstactivity.c Lickevent, Class) 
 
Intent Intent = new Intent (Firstactivity.this, secondactivity) 

This is refers to your the current object. In your case your must have implemented the intent in a inner class clickevent, and thats what it points to.
Activity.this points to the instance's activity for you are currently in.

This is a reference to your current object, and in your case you must implement intent within the inner class clickevent, which points to clickevent rather than the activity you want to pass in.

Activity.this points to an instance of the activity name that you have filled out, and is also a reference.

In fact, this is the basis of Java, I have forgotten.

This is not a problem to use directly in an activity as the current object, and when this is used in an anonymous inner class, the current object becomes the inner class of new, and what you pass in will be activity.this if it is the whole activity.

Button B.setnegativebutton (R.string.cancel, New Dialoginterface.onclicklistener () {public 
 void OnClick ( Dialoginterface dialog, int which) { 
  toast.maketext (alarmactivity.this, "alarm clock starts after 5 seconds", Toast.length_short);  
}; 

So in this case you need to specify which activity, the toast statement moved outside, delete alarmactivity also line.

So still is the netizen suggests, all use into activity.this, clear.

The above is the Android this and activity.this the difference of data collation, follow-up continue to supplement the relevant information thank you for your support of this site!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.