"Android Note 3.6" Android Context understanding and use

Source: Internet
Author: User

Course Background:
Context is a very important concept in Android, for accessing global information, almost all of the underlying components are inherited from the context, understanding the context is very helpful for learning the four basic components of Android.

Core content:
1. Understanding the Context
2. Understanding Application

The role of the Context

Resources for accessing the global

Many common components are inherited from the context, just to facilitate access to resources.

PrivateTextView TV; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); TV=NewTextView ( This);//tv.settext ("Hello Android");Tv.settext (R.string.hello_world);        Setcontentview (TV);        System.out.println (Getresources (). GetText (R.string.hello_world)); ImageView IV=NewImageView ( This);        Iv.setimageresource (R.mipmap.ic_launcher);    Setcontentview (iv); }

Use of application

You can easily share data between multiple activity in the same application without having to pass parameters.

//define a class that inherits from Application Public classAppextendsApplication {PrivateString textData = "Default";  Public voidSettextdata (String textData) { This. TextData =TextData; }     PublicString Gettextdata () {returnTextData; }}/*******TV = (TextView) Findviewbyid (R.id.textview); b to display the *******///set in Mainactivity(APP) Getapplicationcontext (). Settextdata ("Hellomygirl"));//after exiting Mainactivity, open bactivity to get the shared data valueTv.settext ("Shared data is:" + ((APP) Getapplicationcontext ()). Gettextdata ());

"Android Note 3.6" Android Context understanding and use

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.