Android uses global variables to pass data

Source: Internet
Author: User

Application in Android is used to save global variables, and it exists when the package is created, and is released when all activity is destroy off. So when we need global variables, we just have to implement them in application, You can set or read the value of a global variable by invoking the Getapplicationcontext of the context or the Getapplication method of the activity to obtain a Application object.

When application is started, a PID, the process ID, is created, and all activity is run on this process. Then we initialize the global variables when the application is created, all the activity of the same application can fetch the values of these global variables, in other words, we change the values of these global variables in one activity, Then the value of the other activity in the same application will change.

Example: If the input content is set to global variables, then there are two cases.



The implementation method is as follows:

I. Create a new Shared global variable

Create a new class of shared variables Wirelessapp, you need to inherit application

Java code
  1. Package com.wirelessqa.testintent;
  2. Import android.app.Application;
  3. /**
  4. * Inherit application, set global variables
  5. * @author Bixiaopeng 2013-2-18 11:32:19
  6. */
  7. Public class Wirelessapp extends application {
  8. Private String username;
  9. Public String GetUserName () {
  10. Returnusername;
  11. }
  12. Public void Setusername (String username) {
  13. This.username = Username;
  14. }
  15. }

Two. Configure Androidmainifest.xml

When you declare a class of global variables in Androidmainifest.xml, Android creates a globally available instance

Set Android:name= "In the Application property . Wirelessapp "

Three. Calling Global variables

You can use Content.getapplicationconext () anywhere else to get this instance, and then get the global variable

Java code
  1. Package com.wirelessqa.testintent;
  2. Import android.app.Activity;
  3. Import android.content.Intent;
  4. Import Android.os.Bundle;
  5. Import Android.view.View;
  6. Import Android.view.View.OnClickListener;
  7. Import Android.widget.Button;
  8. Import Android.widget.EditText;
  9. Public class Mainactivity extends Activity {
  10. Private EditText edit = null;
  11. Private button button = null;
  12. @Override
  13. protected void OnCreate (Bundle savedinstancestate) {
  14. Super.oncreate (savedinstancestate);
  15. Setcontentview (R.layout.activity_main);
  16. Edit = (EditText) Findviewbyid (R.id.edit);
  17. Button = (button) Findviewbyid (R.id.btn_submit);
  18. Button.setonclicklistener (new Onclicklistener () {
  19. @Override
  20. Public void OnClick (View v) {
  21. String result = Edit.gettext (). toString ();
  22. Get an Application instance
  23. Wirelessapp app = (Wirelessapp) getapplicationcontext ();
  24. Assigning a value to a global variable
  25. App.setusername (result);
  26. Start another activity
  27. Intent Intent = new Intent (mainactivity. This, resultactivity.  class);
  28. StartActivity (Intent);
  29. }
  30. });
  31. }
  32. }

Four. Calling the value of a global variable

Java code
    1. Package com.wirelessqa.testintent;
    2. Import android.app.Activity;
    3. Import Android.os.Bundle;
    4. Import Android.widget.TextView;
    5. /**
    6. * Activity that shows results
    7. *
    8. * @author Bixiaopeng 2013-2-18 11:29:50
    9. */
    10. Public class Resultactivity extends Activity {
    11. private TextView text = null;
    12. @Override
    13. protected void OnCreate (Bundle savedinstancestate) {
    14. super.oncreate (savedinstancestate);
    15. Setcontentview (R.layout.activity_result);
    16. //Get Application instances
    17. Wirelessapp app = (Wirelessapp) getapplicationcontext ();
    18. String result = App.getusername (); //value
    19. Text = (TextView) Findviewbyid (R.id.txt_result);
    20. Text.settext (result);
    21. }
    22. }

Android uses global variables to pass data

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.