English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
This article describes the global variables in Android programming. Shared with everyone for reference, as follows:
Now I am busy every day, and when I get busy, I put writing notes behind. Recently, when writing a program, I suddenly needed to use a global variable, so I wrote a class as usual, declared all variables as static, and used them as global variables. However, when switching Activities, I suddenly found that after assigning in the previous Activity, I could not get it in the later one. At first, I thought there was a problem elsewhere, but later checked and found no problem. This problem troubled me for about an hour, and then I tried a different writing method, using Application. But when using it, I also found this problem. At that time, I really didn't know how to deal with it, and searching online also didn't see how to solve it!
In the end, I changed the initialization location for testing, and this time the assignment did not disappear. However, I still didn't understand why this happened. After multiple tests, I suddenly thought of a question. Because I used different termination methods in these two Activities, one being System.exit(0); and the other being finishi(); I felt that these two methods might be the cause. After testing, it turned out that it was indeed these two methods. If using System.exit(0);, it is equivalent to ending this Acitvity, so all the data it has operated on will no longer exist. The system may think there is no need to exist anymore. Using finishi();, although it is also an exit, it is not to release resources, but to push the current Activity to the background and no longer display it. However, it does not release resources, and when the resources are released, it is decided by the system. Of course, System.exit(0); is not a real system exit, because we may still have other Activities running, but it does release the resources! Why is this? I don't quite understand it!
The problem has basically been resolved up to this point, but there is a problem here. For example, after logging in to my login interface, there is actually nothing left to use, so I want to use Exit to log out. How can I save the username and password after login? At this point, I thought of Intent to pass values, using Intent to pass the value, and then receive it and pass it to the display main page. And the main page only calls Exit when the system exits, so it is enough to initialize the public variables here.
After half a day of effort and testing, the two ways of using public variables mentioned above have finally been resolved! Writing this article is to say that sometimes these problems are very simple to handle, but sometimes some misunderstandings caused by less-known mechanisms may occur, so when writing programs, we need to consider all aspects, just like the value passing we are talking about today, only in this way can we learn something!
Below is a simple test code:
1use static class:
package com.Declare; public class Declare{ public static String UserName=""; }
Once called, it no longer needs to be written out; you can directly call the class name followed by the variable name to invoke it!
2use 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; } }
This is how it is called in the class!
Declare declare = (Declare) getApplicationContext(); declare.getAccounts(); declare.setAccounts("...");
It is necessary to declare a subclass of Application to be able to call successfully:
android:name="com.Declare.Declare"
And according to the editing ideas of Java and C#, it is still recommended to use the second method, which is good for system security! And I have checked some information, and it is also in line with the Android idea, so it is recommended to use the second method and set public variables!
Readers who are interested in more content related to Android can check the special topics on this site: 'Android Development入门与进阶教程', 'Android Debugging Skills and Common Problem Solving Methods Summary', 'Android Multimedia Operation Skills Summary (audio, video, recording, etc.)', 'Summary of Android Basic Component Usage', 'Summary of Android View View Skills', 'Summary of Android Layout Layout Skills', and 'Summary of Android Control Usage'.
I hope the content described in this article will be helpful to everyone in Android program design.
Declaration: The content of this article is from the Internet, and the copyright belongs to the original author. The content is contributed and uploaded by Internet users spontaneously. This website does not own the copyright, has not been manually edited, and does not assume any relevant legal liability. If you find any content suspected of copyright infringement, please send an email to: notice#w3Please replace '#' with '@' when sending an email for reporting, and provide relevant evidence. Once verified, this site will immediately delete the infringing content.