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 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, this involves a life cycle problem. This. getapplicationcontext () obtains the application ProgramContext, 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
It belongs to an activity and will be destroyed and will not exist again when the activity is destroyed. 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 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