The difference between Activity.this and Getapplicationcontext in Android

Source: Internet
Author: User

Reprint: http://www.myexception.cn/android/1968332.html

The difference between Activity.this and Getapplicationcontext in Android

Context-related content is often encountered in Android

A brief discussion of the context: in the statement alertdialog.builder Builder = new Alertdialog.builder (this); , the parameter required to be passed is a context, where we are passing in this, then what does this one refer to? Here this refers to the activity.this, which is the activity of this statement, which is the context of this activity. It's not right to have a lot of friends on the Internet passing in This.getapplicationcontext (). The Alertdialog object is dependent on a view, and the view is corresponding to an activity. So, here comes a life cycle problem, This.getapplicationcontext () takes the context,activity.this of this application is the context of the Activity, the life cycle of the two is different , the life cycle of the former is the entire application, and the latter's life cycle is only the activity in which it resides. And Alertdialog should belong to an activity, when the activity is destroyed it is destroyed, no longer exist; Into This.getapplicationcontext (), it means that its life cycle is the entire application, which clearly exceeds its life cycle, and cannot determine which activity this dialog belongs to (Alertdialog is part of the activity), so the program will error, "Anndroid.view.windowmanager$badtokenexception: Unable to add window--token null was not for a application "prompt cannot be added. So, here we can only use the activity's this.

Here's a look at the difference between the two

(1) For Getapplicationcontext, we can assume that it is a parent class, which belongs to the entire application, and activity.this can be assumed to be a subclass of it, which contains some specific references. Therefore, the general can be used Getapplicationcontext place can be replaced with a specific activity.this.

(2) in the life cycle, the context objects that are obtained through getapplicationcontext will persist as long as the current application is present. For the activity.this context, the context object will be reclaimed by the system as long as the current activity executes the Ondestory method.

(3) In the application scenario, if we execute an action through a context object and want to be active all the time, we should use Getapplicationcontext to get the context, such as the operation of the database. At this point, if Activity.this is used, the database shuts down when the current activity calls the Ondestory method, and the application generates an error.

Getapplicationcontext () Returns the context of the application, the life cycle is the entire application, and the application destroys it before it destroys
The context of the Activity.this returns the current activity, belonging to the activity, destroying the activity and destroying it.

Getbasecontext () returns the context specified by the constructor or Setbasecontext () setting

Summarize:

1.dialog
dialog attached to the activity, so directly with the xxxactivity.this, when the activity disappears dialog also destroyed
2.activity
As we have said above, the direct use of Xxxactivity.this, the return is the current activity instance, the current activity is destroyed, together destroy
3.service,broadcastreceiver
Both are possible.
Summary: It is not recommended to use Getapplicationcontext (), which is related to UI operations, and generally uses the context associated with the activity, the rest of the operation, depending on the length of the life cycle of the existence of the choice

The difference between Activity.this and Getapplicationcontext in Android

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.