In Android, the application sets global variables and transmits values.

Source: Internet
Author: User

From: http://www.cnblogs.com/tiantianbyconan/archive/2012/06/14/2548899.html

Application sets global variables and transmits values

  1. /**
  2. * Rewrite the application, which mainly overrides the oncreate method in it, that is, when the application is created,
  3. * Let's initialize some values. We saw an example in javaeye some time ago. similar to this,
  4. * I made some improvements. I heard that foreign developers are used to initializing some global variables, as if they were in the activity
  5. * Some Classes initialize global variables and encounter NULL pointer exceptions. Of course, I have never encountered any exceptions.
  6. * If this method is used for initialization, you can avoid possible errors.
  7. *
  8. * When the application is started, a PID is created, that is, the process ID, and all the activities will run on this process.
  9. * If we initialize global variables when the application is created, can all the activities obtain these variables?
  10. * Global variables. Further, if we change the values of these global variables in an activity
  11. * Is the value changed? Isn't it a value?
  12. * OK, so let's test the example below...
  13. * @ Author Yong. Wang
  14. *
  15. */
  16. Public class myapplication extends application {
  17. Private string name;
  18. @ Override
  19. Public void oncreate (){
  20. Super. oncreate ();
  21. Setname (name); // initialize the global variable
  22. }
  23. Public String getname (){
  24. Return name;
  25. }
  26. Public void setname (string name ){
  27. This. Name = Name;
  28. }
  29. Private Static final string name = "myapplication ";
  30. }

// 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:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Package = "com. hisoft. app"
  4. Android: versioncode = "1"
  5. Android: versionname = "1.0" type = "codeph" text = "/codeph">
  6. <Application Android: icon = "@ drawable/icon" Android: Label = "@ string/app_name"
  7. Android: Name = ". myapplication"> here, we set the default application we have been using to our own myapplication.
  8. <Activity Android: Name = ". myfirstactivity"
  9. Android: Label = "@ string/app_name">
  10. <Intent-filter>
  11. <Action Android: Name = "android. Intent. Action. Main"/>
  12. <Category Android: Name = "android. Intent. Category. launcher"/>
  13. </Intent-filter>
  14. </Activity>
  15. <Activity Android: Name = ". mysecondactivity"> </activity>
  16. </Application>
  17. <Uses-SDK Android: minsdkversion = "8"/>
  18. </Manifest>

After the xml configuration file runs Android: Name = ". myapplication">, the process ID is allocated. Next, we will run our activity.

  1. Public class myfirstactivity extends activity {
  2. Private myapplication app;
  3. @ Override
  4. Public void oncreate (bundle savedinstancestate ){
  5. Super. oncreate (savedinstancestate );
  6. Setcontentview (R. layout. Main );
  7. APP = (myapplication) getapplication (); // obtain the myapplication
  8. Log. E ("myfirstactivityoriginal", app. getname (); // take out the global variables we put in the process to see if the values we have set
  9. App. setname ("is cool"); // OK, now we have modified
  10. Log. E ("myfirstactivitychanged", app. getname (); // check whether the value has changed.
  11. Intent intent = new intent (); // more importantly, we can see whether the initialization value is obtained or modified in other activities.
  12. Intent. setclass (this, mysecondactivity. Class );
  13. Startactivity (intent );
  14. }
  15. }

After running the above, you have to jump to this activity.

  1. Public class mysecondactivity extends activity {
  2. Private myapplication app;
  3. @ Override
  4. Protected void oncreate (bundle savedinstancestate ){
  5. Super. oncreate (savedinstancestate );
  6. Setcontentview (R. layout. Main );
  7. APP = (myapplication) getapplication (); // get the application
  8. Log. E ("mysecondactivity", app. getname (); // obtain the Global Value
  9. }
  10. }

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.

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.