Context is an interface to access global information, such as application resources (such as pictures, strings, etc. ), and some commonly used components inherit from the context, such as Activity and Service.
If you create a textView with Java code, the first setText () method of textView passes in a string directly, and the second setText () method passes in a plastic ID (hello_world under values \ \ strings.xml, namely:
[java] View Plain Text
Public class MainActivity extends ActionBarActivity {
Private TextView textView
@ Overlay
Protected void oncreate (bundle saved instancestate) (
super . oncreate(savedInstanceState);
//setContentView(r . layout . activity _ main);
textView = new textView(main activity . this);
//textview . settext(" Hello geo storm ");
textview . settext(r . string . hello _ world);
SetContentView (text view);
}
}
In the above code, double-click the first setText () method and press F4 to see the constructor. The entry parameter is a string.
[java] View Plain Text
public final void setText(char sequence text){
setText(text,mBufferType);
}
Double-click the first setText () method and press F4 to view the constructor. The entry parameter is resid, its internal setText () is the first setText () method, and getContext () is MainActivity.this! ! !
[java] View Plain Text
public final void setText(int resid){
setText(getContext()。 getResources()。 getText(resid));
}
After reading the constructor, we can also write according to the original text in the constructor:
[java] View Plain Text
//textview . settext(r . string . hello _ world);
textview . settext(getApplicationContext()。 getResources()。 getText(r . string . hello _ world));
Similarly, we can also access the picture resources:
[java] View Plain Text
Public class MainActivity extends ActionBarActivity {
Private TextView textView
Private ImageView imageView
@ Overlay
Protected void oncreate (bundle saved instancestate) (
super . oncreate(savedInstanceState);
//setContentView(r . layout . activity _ main);
//textView = new textView(main activity . this);
//textview . settext(" Hello geo storm ");
//textview . settext(r . string . hello _ world);
//textview . settext(getApplicationContext()。 getResources()。 getText(r . string . hello _ world));
ImageView = new imageview (this); //You can use this directly.
imageview . set image resource(r . MIP map . IC _ launcher);
setContentView(imageView);
}
}
2. Application and application description
Now we use context to share information between components. Create a App.java class to pass data, inherit from the application, add a string variable, and add a read-write constructor, as shown below:
[java] View Plain Text
Package com.example.lhb.context;
Import android.app.application;
Common class application extension application {
public String S = " DefaultString
Public empty set (string) (
S = s
}
Common string getS() {
Return to s;
}
}
Next, configure Android manifest: add Android: name = ". App "is added to the application, and another activity named Main2 is added. Add the same configuration as MainActivity in the configuration file, where
[java] View Plain Text
& lt? Xml version =" 1.0 "encoding ="utf-8"? & gt
& lt listing xmlns: Android = "/apk/res/Android"
package = " com . example . lhb . context " & gt;
& lt application
android:name= "。 Application "
android:allowBackup="true "
Android:icon = " @ MIP map/IC _ launcher "
Android:label = " @ string/app _ name "
Android:theme = " @ style/app theme " & gt;
& lt activities
android:name= "。 Main activities "
Android:label = " main 1 " & gt;
& lt intention filter & gt
& ltaction Android:name = " Android . intent . action . main "/& gt;
& lt category Android: name = "android.intention.category.launcher"/>
& lt/intent-filter & gt;
& lt/activity & gt;
& lt activities
Android:name = " . main activity 2 activity "
Android:label = " main 2 " & gt;
& lt intention filter & gt
& ltaction Android:name = " Android . intent . action . main "/& gt;
& lt category Android: name = "android.intention.category.launcher"/>
& lt/intent-filter & gt;
& lt/activity & gt;
& lt/application & gt;
& lt/manifest & gt;
In Main 1, add the following code to Main2:
[java] View Plain Text
Package com.example.lhb.context;
Import android.support.v7.app.actionabaractivity;
Import android.os.bundle;
Import android.view.menu;
Import android.view.menuitem;
Import android.view.view;
Import android.widget.edittext;
Import android.widget.imageview;
Import android.widget.textview;
Public class MainActivity extends ActionBarActivity {
Private TextView textView
Private EditText editText
@ Overlay
Protected void oncreate (bundle saved instancestate) (
super . oncreate(savedInstanceState);
setContentView(r . layout . activity _ main);
textView =(textView)findViewById(r . id . textView);
editText =(editText)findViewById(r . id . editText);
//Read the default string
TextView.setText("*** Enjoy the data as follows: "+((app) getapplicationcontext ())). gets());
findViewById(R.id.btnSave)。 SetOnClickListener (new view. OnClickListener() {
@ Overlay
Public void onClick (view v)
((App)getApplicationContext())。 setS(editText.getText()。 toString());
Textview.settext (string.format ("* * * data is:% s", edittext.gettext (). tostring()));
}
});
}
}
The effect is as follows:
3. Application life cycle
Override the constructor and execute it. It turns out that App.java's onCreate () function is executed before any active onCreate () function, which is very useful for our initialization.
[java] View Plain Text
Package com.example.lhb.context;
Import android.app.application;
Common class application extension application {
public String S = " DefaultString
Public empty set (string) (
S = s
}
Common string getS() {
Return to s;
}
@ Overlay
//Execute at creation time
public void onCreate() {
super . oncreate();
system . out . println(" App OnCreate ");
}
@ Overlay
//Last execution, generally not executed, only executed in the simulation environment.
Public void onTerminate() {
super . on terminate();
system . out . println(" App on termin ate ");
}
@ Overlay
//Execute when memory is insufficient
public void onLowMemory() {
super . onlow memory();
}
@ Overlay
//Executed when the memory is cleared.
Public void(int level) in memory (
Super. Ontario (level);
}
}