Global variable problem analysis in Android programming _android

Source: Internet
Author: User

This example describes the global variables in Android programming. Share to everyone for your reference, specific as follows:

Now every day is busy, and a busy, put the notes on the back, recently in the writing program, suddenly to use the global variable, in the previous way, wrote a class, and then the variables are declared as static variables, and then as a global variable to use, but when the activity is switched, Suddenly found that in the previous Acitivty assignment, but in the back is not available, just started to think there are other places have problems, but then check found that there is no problem, this problem plagued one hours, and then another way of writing, is the use of application, but in the use of the time , also found that there is such a problem, at that time really feel do not know how to deal with, online search data also did not see how to solve!

In the end I changed the initialization to a place to test, this assignment did not disappear, but at this time did not understand why this is, and then many tests, suddenly thought of a problem, because I used in these two activity is not the same way to end, one for system.exit (0); One is Finishi (); The sensation may have been caused by these two ways, and later tested, indeed, by these two ways, If the use of System.exit (0), is tantamount to the end of the acitvity, so that the data he has been operating no longer exist, the system may think that there is no need for it, the use of Finishi (), although it is also the exit, but this is not the release of resources , but the current activity to the background, no longer show, but he does not release resources, specific resources when the release, determined by the system, Of course system.exit (0); This form is not really an exit system, because we may have other activity running, but he did release the resources! This is why, do not understand!

To solve the problem here, but there is a problem, so for example, I login interface, after the completion of the login, in fact, there is no use, I would like to use exit, how to put his login after the user name, password to save it? This time, thought of intent this, pass the value, use intent to pass the value, then receive, upload the main page of the display, and the main page, only when the system exit will call exit, therefore, this in the initialization of public variables, it can be.

After half a day's effort has tested, the above mentioned two kinds of public variable use way, finally all can! To write this article is to say, in fact, sometimes these problems are very simple to deal with, but sometimes some of the mechanisms of the problem may be misunderstood, so in the process of writing, we need to think about all aspects of the problem, as today the value of the same, only such a son can learn things!

The following is a simple test code:

1, the use of static classes:

Package com. Declare;
public class declare{public
  static String username= "";
}

The call is no longer written, and the class name is used directly. Variable names can be invoked!

2. Use Application

Package com. Declare;
Import android.app.Application;
public class Declare extends application {
  private String straccounts = "";
  Operation account public
  void Setaccounts (String accountsno) {
    this.straccounts = Accountsno;
  }
  Public String getaccounts () {return
    this.straccounts;
  }
  Operation Password public
  void Setaccountspassword (String passWord) {
    this.straccountspwd = PassWord;
  }
  Public String Getaccountspassword () {return
    this.straccountspwd
  }}


Called in the class is such a child call!

Declare Declare = (Declare) getapplicationcontext ();
Declare.getaccounts ();
Declare.setaccounts ("");

To be able to invoke successfully, you must declare application subclasses:

Android:name= "com. Declare.declare "

And according to the Java and C # of the kind of editing ideas, or suggest the use of the second Test, so for the security of the system is good! And I checked some data to show that this is also consistent with the idea of Android, so it is recommended to use the second way, set the public variable!

For more information on Android-related content readers can view the site topics: "Android Development Introduction and Advanced Course", "Android debugging techniques and common problems solution summary", "Android Multimedia operating skills Summary (audio, video, recording, etc.)", " Android Basic Components Usage Summary, Android View tips Summary, Android layout layout tips and Android Control usage summary

I hope this article will help you with the Android program.

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.