[Development Diary of Android in a row from scratch] (12)-data transmission between Android Components

Source: Internet
Author: User

[Development Diary of Android in a row from scratch] (12)-data transmission between Android Components

We have components, so we lack a channel to transmit information between components. Using Intent as a carrier is a king of practices. In addition, the file system can be used for data sharing. You can also use the Application to set global data and use components to control data.

 

I. Intent data transfer II. File System data sharing

 

3. Set global data for Application

In java, we usually use static variables when using global variables, and then the public type, so that all classes can use these variables. Of course, this can also be done on Android. However, here we can use Application to manage these variables. The Application class is a base class, which is used to obtain the state of the entire Application. We can inherit or implement this class by ourselves. When you want to use your own extended application class, you only need. in xml, the <application> label in the name application defines the class. The result is: when your application or the process where the package is located is created, this class will be instantiated.

The method used is very simple. It inherits the Application class and then overwrites it. It mainly overrides the onCreate method in it, that is, the value of the variable is initialized at the time of creation. Then the variable can be operated on in each file of the entire application.

When the Application is started, the system creates a PID, that is, the process ID, and all the activities will run on this process. Then we initialize the global variables when the Application is created. All the activities of the same Application can obtain the values of these global variables. In other words, if we change the values of these global variables in an Activity, the values of other activities in the same application will change. In this way, global variables are controlled. (Of course, application is not limited here. It can be used with developers to set more functions)
The following is a small example:

Public class MyApp extends Application {private String learn; public String getLearn () {return learn;} public void setLearn (String s) {this. learn = s ;}@ Override public void onCreate () {// TODO Auto-generated method stub super. onCreate (); setLearn ("cpacm"); // initialize the global variable }}

The above is a simple use that inherits the Application class. We just created a new string variable. It is neither global nor static. to use it, you must use MyApp.

Next let's take a look at how we use it:

Public class mainActivity extends Activity {private MyYApp myApp; @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); myApp = (MyYApp) getApplication (); // obtain the Custom Application YApp Log. d ("TEST", "InitLabel:" + myApp. getLearn (); // take out the global variables we put in the process to see if the value myApp was set. setLearn ("ahaha"); // modify the Log. d ("TEST", "ChangeLabel:" + myApp. getLearn (); // check whether the value has changed }}

Finally, register your own App in the configuration file.

<! -- Here, set the default Application to your own MyApp --> <application android: name = "MyApp".../>
Iv. component control data

V. Conclusion

Call ~ It has been a month since I wrote my first blog, and the time has passed without knowing it. During the period, I wrote more than a dozen blog posts intermittently. However, if I write well or not well, I feel that I have gained a lot from it, compared with the previous one who wants to learn what it is, the current comprehensive learning is much better.

So far, the components of the Android beginners are basically completed. Of course, there is still a lot of content to go into. You need to explore this aspect by yourself. The next step is the control part of Android. The amount and the thought of so many Control headers are big, but the basic principles are the same ~ Okay. If you have any questions, contact me for further study,In the end, we hope you will Have a good day.

 

 

==========================================================

Author: cpacm
(Http://www.cnblogs.com/cpacm/p/3946586.html)

 




Related Article

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.