Analysis of global variables in Android programming

This article describes the global variables in Android programming. Share with you for your reference, as follows:

Now I'm busy every day, and as soon as I'm busy, I put my notes behind me. Recently, when I was writing a program, I suddenly wanted to use global variables. I wrote a class according to the previous method, and then declared the variables as static variables, and then used them as global variables. However, when switching activities, I suddenly found that, After assigning a value to the previous acitivty, it was not available later. At first, it was thought that there was a problem in other places, but later it was found that there was no problem. This problem bothered for about an hour. Later, it was written in another way, that is, using application, but it was also found that there was such a problem when using it. 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!

Finally, I changed the initialization to another place for testing, and the assignment did not disappear, but I still don't understand why. After testing again and again, I suddenly thought of a problem, because I used different ending methods in the two activities, one is system exit(0); One is finishi (); I think it may be caused by these two methods. After testing, it is really caused by these two methods. If you use system exit(0); In this way, it is equivalent to ending the acitvity, so the data he has operated will no longer exist. The system may think that it is not necessary to exist. Use finishi(); In this case, it is also an exit, but this does not release resources. It just pushes the current activity to the background and will not be displayed. However, it does not release resources. The specific time of resource release is determined by the system. Of course, system exit(0); This form is not a real exit from the system, because we may have other activities running, but it does release resources! Why? I don't understand!

Here, the problem has been basically solved, but there is a problem. Therefore, for example, my login interface is actually useless after login. I want to use exit. How can I save his login user name and password? At this time, I think of intent. Pass the value. Use intent to pass the value, and then receive it and send it to the displayed main page. Moreover, the main page will call exit only when the system exits. Therefore, it is OK to initialize the public variable in this.

After half a day's effort and testing, the use of the two public variables mentioned above is finally OK! Writing this article is to say that sometimes these problems are very simple to deal with, but sometimes some mechanism problems that we don't know very well may cause misunderstanding. Therefore, when writing programs, we need to think of all aspects of problems, just like today's value transmission. Only in this way can we learn something!

Here is a simple test code:

1. Use static classes:

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

2. Using application

It is called in the class.

To successfully call, you must declare a subclass of application:

Moreover, according to the editing ideas of Java and C #, it is recommended to use the second test, which is good for the security of the system! And I checked some data to show that this is also in line with the idea of Android, so I suggest using the second method to set public variables!

For more Android related content, readers who are interested can view the topics on this site: introduction and advanced tutorial of Android development, summary of Android debugging skills and common problem solutions, summary of Android multimedia operation skills (audio, video, recording, etc.), summary of Android basic component usage, summary of Android view skills Android layout skills summary and Android control usage summary

I hope this article will help you in Android programming.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>