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