What is the context in Android and how does it use

Source: Internet
Author: User

This is stackoverflow on the answer of a netizen:

Original address: Http://stackoverflow.com/questions/3572463/what-is-context-in-android

As the name suggests, its the context of the application/object. It lets newly created objects understand what have been going on. Typically you-call it-get information regarding another part of the Your program (activity, Package/application)

You can get the context by invoking getApplicationContext() , getContext() , getBaseContext() or (if in the this activity Class).

Typical uses of context:

    • Creating new objects : Creating new views, adapters, listeners:

       1  TextView TV =  TextView (GetContext ( )); 2  listadapter adapter = new 
       Simplecursoradapter (Getapplicationcontext (), ...); 
    • accessing Common Resources : Services like Layout_inflater_service, sharedpreferences :

       1  context.getsystemservice (Layout_ Inflater_service)  2  Getapplicationcontext (). Getsharedpreferences (* name*, *mode*); 

       

    • Accessing components implicitly : Regarding content providers, broadcasts, intent

    getApplicationContext().getContentResolver().query(uri, ...);

What is the context in Android and how does it use

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.