Android uses global variables

Source: Internet
Author: User
Now I am busy every day. When I get busy, I put my notes in the back. Recently I am writing Program When the global variables were suddenly used, a class was written according to the previous method, and all the variables were declared as static variables, and then used as global variables, however, during activity switching, we suddenly found that after assigning values in the previous acitivty, we could not get the values at the end. At first we thought there was a problem in other places, but later I checked and found that there was no problem. This problem had plagued me for an hour. Later I used the application, but I also found this problem, at that time, I really didn't know how to deal with it. I didn't see how to solve it when I checked the information on the Internet! In the end, I changed the initialization to another place for testing. The assignment did not disappear, but I still don't understand why. I suddenly thought of a problem after multiple tests, because I have used different end methods in these two activities, one is system. exit (0); one is finishi (); it may be caused by these two methods. After testing, it is indeed caused by these two methods. the exit (0); method is equivalent to killing the acitvity, so the data he has operated on will no longer exist. The system may think there is no need, if you use finishi ();, although it is also exited, it does not release resources, but pushes the current activity to the background and does not display it, but does not release resources, the system determines when resources are released. Of course, system. exit (0); this form does not really exit the system, because we may have other activities running, but it does release the resources! This is why, not quite clear! The problem is basically solved here, but there is a problem, so for example, after I log on to the interface, it is actually useless, and I want to exit it, how can I save the user name and password after his login? At this time, I thought of the intent, passed the value, and used the intent to pass the value to the past, and then received and sent to the display home page, and the home page, exit is called only when the system exits. Therefore, you can initialize the public variable. After half a day of effort and testing, the above two public variables can be used at last! This article is intended to be written. In fact, sometimes these problems are very simple to handle, but sometimes some unknown mechanisms may cause misunderstandings. Therefore, when writing a program, we need to think about all aspects of the problem, just like passing values today. Only in this way can we learn things! Below is a simple test Code : 1. Use static class: Package COM. declare; public class declare {public static string username = "";} copy the code and call it. You can directly use the class name. the variable name can be called! 2. Use the application code 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;} the copied code is called like this in the class! Declare declare = (declare) getapplicationcontext (); declare. getaccounts (); declare. setaccounts (""); To successfully call the copied code, you must declare the subclass of the application: Android: Name = "com. declare. declare "and we recommend that you use the second method based on the Java and C # editing ideas. This is good for system security! In addition, I checked some information and showed that this is also in line with the idea of Android. Therefore, we recommend that you use the second method to set public variables!
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.