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.