Android -- Context (Application)
Interface to global information about an application environment. this is an abstract class whose implementation is provided by the Android system. it allows access to application-specific resources and classes, as well as up-callfor application-level operations such as launching activities, broadcasting and processing intents
It is an interface used to access global information (image resources and information resources). Some common components inherit the Context interface.
Application
Like Activity and Service, it is a system component of the android framework. When the android program starts, the system creates an application object to store some information about the system. Generally, we do not need to specify an Application. In this case, the system automatically creates an Application for us. If you need to create your own Application, it is also easy to create a class that inherits the Application and registers it in the application tag of manifest (you only need to add a name attribute to the Application tag to set the name of your Application ).
The android system creates only one Application Class Object for each program running. Therefore, the Application is a class in singleton mode. the life cycle of the application object is the longest in the whole program, and its life cycle is equal to the life cycle of the program. Because it is a global Singleton, the objects obtained in different activities and services are all the same object. Therefore, you can use the Application to perform operations such as data transmission, data sharing, and data caching.
Next, we use an instance to share Application data.
1. Create an object that inherits the Application
package com.example.learncontext;import android.app.Application;public class App extends Application { private String name = "default"; public String getName() { return name; } public void setName(String name) { this.name = name; }}
2. We need to configure AndroidManifest. xml
12 lines in the Code. here we need to configure the path for creating the Application object.
From lines 17 to 35, we can see that two activities are configured and they are all entry programs. For details, see the code in intent-filter. The purpose is to display two app entry icons on the mobile phone to facilitate Application data sharing.
3. Below are two views of the Activity. One textview, one editview, and one button are added to each VIEW page.
4. The Code in the following two activities
Package com. example. learncontext; import android. OS. bundle; import android. app. activity; import android. view. menu; import android. view. view; import android. widget. editText; import android. widget. textView; public class BMainActivity extends Activity {private TextView textView; private EditText editText; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_bmain); textView = (TextView) findViewById (R. id. textView1); editText = (EditText) findViewById (R. id. editText1); textView. setText ("the obtained shared data is:" + (App) getApplicationContext ()). getName (); findViewById (R. id. button1 ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {(App) getApplicationContext ()). setName (editText. getText (). toString (); textView. setText ("the obtained shared data is:" + (App) getApplicationContext ()). getName () ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the Menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. bmain, menu); return true ;}}
Package com. example. learncontext; import android. app. activity; import android. app. application; import android. OS. bundle; import android. view. menu; import android. view. view; import android. widget. editText; import android. widget. textView; public class MainActivity extends Activity {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. textView1); editText = (EditText) findViewById (R. id. editText1); textView. setText ("the obtained shared data is:" + (App) getApplicationContext ()). getName (); findViewById (R. id. button1 ). setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {(App) getApplicationContext ()). setName (editText. getText (). toString (); textView. setText ("the obtained shared data is:" + (App) getApplicationContext ()). getName () ;}}) ;}@ Override public boolean onCreateOptionsMenu (Menu menu) {// Inflate the Menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. main, menu); return true ;}}
30 lines of code (App) getApplicationContext () in the program are converted to an App object, and properties in the object are obtained. The put or get method is used to store and read data sharing.
5. The test diagram is as follows: