From: http://www.cnblogs.com/tiantianbyconan/archive/2012/06/14/2548899.html
Application sets global variables and transmits values
- /**
- * Rewrite the application, which mainly overrides the oncreate method in it, that is, when the application is created,
- * Let's initialize some values. We saw an example in javaeye some time ago. similar to this,
- * I made some improvements. I heard that foreign developers are used to initializing some global variables, as if they were in the activity
- * Some Classes initialize global variables and encounter NULL pointer exceptions. Of course, I have never encountered any exceptions.
- * If this method is used for initialization, you can avoid possible errors.
- *
- * When the application is started, a PID is created, that is, the process ID, and all the activities will run on this process.
- * If we initialize global variables when the application is created, can all the activities obtain these variables?
- * Global variables. Further, if we change the values of these global variables in an activity
- * Is the value changed? Isn't it a value?
- * OK, so let's test the example below...
- * @ Author Yong. Wang
- *
- */
- Public class myapplication extends application {
- Private string name;
- @ Override
- Public void oncreate (){
- Super. oncreate ();
- Setname (name); // initialize the global variable
- }
- Public String getname (){
- Return name;
- }
- Public void setname (string name ){
- This. Name = Name;
- }
- Private Static final string name = "myapplication ";
- }
// OK. The application has been created, but we should add the application myapplication to be run in the configuration file applicationmanifest. xml. Modify the following:
- <? XML version = "1.0" encoding = "UTF-8"?>
- <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
- Package = "com. hisoft. app"
- Android: versioncode = "1"
- Android: versionname = "1.0" type = "codeph" text = "/codeph">
- <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"
- Android: Name = ". myapplication"> here, we set the default application we have been using to our own myapplication.
- <Activity Android: Name = ". myfirstactivity"
- Android: Label = "@ string/app_name">
- <Intent-filter>
- <Action Android: Name = "android. Intent. Action. Main"/>
- <Category Android: Name = "android. Intent. Category. launcher"/>
- </Intent-filter>
- </Activity>
- <Activity Android: Name = ". mysecondactivity"> </activity>
- </Application>
- <Uses-SDK Android: minsdkversion = "8"/>
- </Manifest>
After the xml configuration file runs Android: Name = ". myapplication">, the process ID is allocated. Next, we will run our activity.
- Public class myfirstactivity extends activity {
- Private myapplication app;
- @ Override
- Public void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- APP = (myapplication) getapplication (); // obtain the myapplication
- Log. E ("myfirstactivityoriginal", app. getname (); // take out the global variables we put in the process to see if the values we have set
- App. setname ("is cool"); // OK, now we have modified
- Log. E ("myfirstactivitychanged", app. getname (); // check whether the value has changed.
- Intent intent = new intent (); // more importantly, we can see whether the initialization value is obtained or modified in other activities.
- Intent. setclass (this, mysecondactivity. Class );
- Startactivity (intent );
- }
- }
After running the above, you have to jump to this activity.
- Public class mysecondactivity extends activity {
- Private myapplication app;
- @ Override
- Protected void oncreate (bundle savedinstancestate ){
- Super. oncreate (savedinstancestate );
- Setcontentview (R. layout. Main );
- APP = (myapplication) getapplication (); // get the application
- Log. E ("mysecondactivity", app. getname (); // obtain the Global Value
- }
- }
Copy code
OK. Check the value. Of course, I have already run it,
Myfirstactivityoriginal myapplication
Myfirstactivitychanged is cool
Mysecondactivity is cool
Check whether it is very exciting. Of course, when you exit and run it again, it may be 2nd or 3... The three outputs may be "is cool", which means you didn't kill the process.