Android Studio development basics-Context usage instructions
1. Context description
Context is an interface used to access global information, such as application resources (slice, String, etc.). Some common components inherit from Context, such as Activity and Service.
For example, you can use Java code to create a textView. The first setText () method of textView is used to directly input a string, and the second setText () method is used to input an integer id (in values \ strings. hello_world under xml, that is: Hello world! ), And change setContextView to setContextView (textView ).
public class MainActivity extends ActionBarActivity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.activity_main); textView=new TextView(MainActivity.this); //textView.setText(Hello GeoStorm); textView.setText(R.string.hello_world); setContentView(textView); }}
In the above Code, double-click the first setText () method and press F4 to see the constructor. The entry parameter is a string.
public final void setText(CharSequence text) { setText(text, mBufferType); }
Double-click the first setText () method and press F4 to see the constructor. The entry parameter is resid, and its internal setText () is the first setText () method, getContext () that is, the MainActivity passed in when TextView is created. this !!!
public final void setText(int resid) { setText(getContext().getResources().getText(resid)); }
After reading the constructor, we can also follow the original syntax in the constructor:
//textView.setText(R.string.hello_world); textView.setText(getApplicationContext().getResources().getText(R.string.hello_world));
Similarly, we can also access image resources:
Public class MainActivity extends ActionBarActivity {private TextView textView; private ImageView imageView; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); // setContentView (R. layout. activity_main); // textView = new TextView (MainActivity. this); // textView. setText (Hello GeoStorm); // textView. setText (R. string. hello_world); // textView. setText (getApplicationContext (). getResources (). getText (R. string. hello_world); imageView = new ImageView (this); // you can use this imageView directly. setImageResource (R. mipmap. ic_launcher); setContentView (imageView );}}
2. Application and description
Now we use Context to share information between components. Create an App. java class to pass data, inherit from Application, add a string variable, and add a read/write constructor, as shown below:
package com.example.lhb.context;import android.app.Application;public class App extends Application { public String S=DefaultString; public void setS(String s) { S = s; } public String getS() { return S; }}
Next, configure AndroidManifest: Add android: name =. App to the Application and add another Activity named Main2. Add the same configuration as MainActivity in the configuration file, which indicates that two applications appear on the mobile phone desktop.
Add the same code as Main1 and Main2:
Package com. example. lhb. context; import android. support. v7.app. actionBarActivity; 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; @ Override protected void onCreate (Bundle savedInstanceState) {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 (the shared data is: + (App) getApplicationContext ()). getS (); findViewById (R. id. btnSave ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {(App) getApplicationContext ()). setS (editText. getText (). toString (); textView. setText (String. format (the shared data is % s, editText. getText (). toString ()));}});}}
The effect is as follows:
3. Application Lifecycle
Rewrite the constructor and execute it. The result shows that the onCreate () function of App. java is executed before the onCreate () function of any Activity, which is very useful for initialization.
Package com. example. lhb. context; import android. app. application; public class App extends Application {public String S = DefaultString; public void setS (String s) {S = s;} public String getS () {return S ;} @ Override // execute public void onCreate () {super. onCreate (); System. out. println (App OnCreate) ;}@ Override // It is executed at the end. Generally, it is not executed. Only public void onTerminate () {super. onTerminate (); System. out. println (App on Terminate) ;}@ Override // execute public void onLowMemory () {super. onLowMemory () ;}@ Override // execute public void onTrimMemory (int level) {super. onTrimMemory (level );}}