Differences between this and this. getapplictioncontext when context is involved in Android

Source: Internet
Author: User

In Android, content related to context is often encountered. In this blog, we will discuss context: in the alertdialog statement. builder = new alertdialog. in Builder (this);, the parameter to be passed is a context. Here we pass in this. So what exactly does this refer? Here this refers to activity. This is the activity where the statement is located. This is the context of the activity. This. getapplicationcontext () is not supported by many online users. The alertdialog object depends on a view, and the view corresponds to an activity. Therefore, a life cycle issue is involved here. This. getapplicationcontext () obtains the applicationProgramContext, activity. this takes the context of the activity, and the lifecycles of the two are different. The lifecycle of the former is the whole application, and that of the latter is only the activity of the activity. Alertdialog should belong to an activity. When the activity is destroyed, it will be destroyed and will not exist any more. However, if this is input. getapplicationcontext () indicates that its life cycle is the entire application, which is clearly beyond its life cycle. Therefore, here we can only use this of activity.

 

The difference means different lifecycles.

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.