The difference between GetContext (), getapplicationcontext (), getbasecontext ()
Source: Internet
Author: User
<span id="Label3"></p> <ul> <li><li><p>1, View.getcontext (): Returns The context the View is currently running IN. Usually the currently active Activity.</p></li></li> <li><li>Context of the current activity</li></li> <li><li><p>2, Activity.getapplicationcontext (): Returns The context for the entire application (the process all the activities is run Ning inside Of). Use this instead of the current Activity context if you need a context tied to the lifecycle of the entire application, no t just the current Activity.</p></li></li> <li><li>Context of the application</li></li> <li><li><p>3, contextwrapper.getbasecontext (): If You need access to a context from within another context, you use a contextwrapper. The Context referred to from inside this contextwrapper is accessed via Getbasecontext ().</p></li></li> <li><li><code>getBaseContext()</code>Is the method of <code>ContextWrapper</code> . <code>ContextWrapper</code>and is, "<em>proxying implementation of the context that simply delegates all of its calls to another Context. Can is subclassed to modify behavior without changing the original Context.</em>"</li></li> </ul><p><p> </p></p><p><p>Unlike the life cycle of 1 and 2, the life cycle of the former depends on the activity in which it is based, and the life cycle of the latter depends on the entire Application. So when new Alertdialog.builder (getapplicationcontext ()) is wrong, Aletrdialog relies on a view, and the view corresponds to an activity, If the Getapplicationcontext () is passed in, its life cycle is the entire application, and when exiting the current activity, it will be reported unable to add window--the token null is the error for a application , the context of the current activity should be passed IN.</p></p><p><p></p></p><p><p>The difference between GetContext (), getapplicationcontext (), getbasecontext ()</p></p></span>
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