Android uses global variables to pass data. android global variables

Source: Internet
Author: User

Android uses global variables to pass data. android global variables

In android, the Application is used to save global variables. It exists when the package is created and is released only after all the activities are destroy. So when we need global variables, we only need to implement them in the application, and obtain an Application object by calling the getApplicationContext of Context or the getApplication method of Activity, you can set or read the value of the global variable.

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.

Instance test: If you set the content in the input to a global variable, there are two scenarios.



 

The specific implementation method is as follows:

1. Create a shared global variable

Create a WirelessApp class with shared variables and inherit the Application

Java code  
  1. Package com. wirelessqa. testintent;
  2. Import android. app. Application;
  3. /**
  4.  
  5. * Inherit application and set global variables
  6.  
  7. * @ Author bixiaopeng 2013-2-18 11:32:19 AM
  8.  
  9. */
  10. Public class WirelessApp extends Application {
  11. Private String username;
  12. Public String getUsername (){
  13. Returnusername;
  14. }
  15. Public void setUsername (String username ){
  16. This. username = username;
  17. }
  18. }

 

2. Configure AndroidMainifest. xml

Declare the global variable class in AndroidMainifest. xml, then Android will create a global available instance

Set android: name = ". WirelessApp" in the Application attribute"

3. Call global variables

You can use Content. getApplicationConext () anywhere else to obtain this instance and then obtain global variables.

 

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. // Obtain the application instance
  23. WirelessApp app = (WirelessApp) getApplicationContext ();
  24. // Assign a value to the 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. }

 

4. Call the global variable value

 

 

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 displays the result
    7. *
    8. * @ Author bixiaopeng 2013-2-18 11:29:50 AM
    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. // Obtain the application instance
    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. }

In android, how does one use intent to transmit data by passing variables?

Private static final String DATABASE_NAME = "test. db ";
Private static final int DATABASE_VERSION = 1;

Public class DatabaseHelper extends SQLiteOpenHelper {
Public DatabaseHelper (Context context ){
Super (context, DATABASE_NAME, null, DATABASE_VERSION );
}

In this way, you can define the parameters first. You can try again. I am also engaged in android development. If you have any questions, please contact me. My QQ account is 379371398 and I hope to adopt it.


How to use global variables in Android

Check whether global variables can be used in android. The Java developer must have used global variables. The usage is nothing more than defining a static variable and the public type, so that you can directly call it in other classes.

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.