Why is global variable passing not recommended in Android development?

Source: Internet
Author: User

In Android development, Intent is generally used to send parameters to the Activity. When complicated objects need to be uploaded, we tend to use global variables (static variables or Application attributes ). But in fact, this is a hidden danger. It is related to the lifecycle of the Activity. I have encountered this problem recently. Write it here.

The general situation is as follows: In ActivityA, click the button to start ActivityB. At the same time, a big data object is required to be uploaded, so I am too lazy to serialize this object, so I directly created a global variable ActivityB. param is written in, in ActivityB. onCreate reads and displays information, and compilation runs properly. It seems that no problems have been found after more than half a month. One day after it was sent to the customer, a null pointer error was found in umeng's background. After carefully analyzing the stack code, the error was found in ActivityB. an error occurred while reading the global variable in onCreate, that is, the global variable returned is null!

If the global variable is null, the process is killed and re-created due to insufficient memory. According to common sense analysis, ActivityA is giving ActivityB. activityB is started immediately after the param value is assigned. This process is very short and cannot be killed so quickly. Therefore, ActivityB is theoretically used. activityB should be read in onCreate. param.

In fact, ActivityA gives ActivityB. after ActivityB is enabled for param assignment, ActivityB. onCreate can indeed read ActivityB. however, ActivityB cannot always be at the front end. Once the task of ActivityB is switched to the back end (if a call is received ), the system can KILL the process in which ActivityB is located when the memory is insufficient. When the task in which ActivityB is located is switched back to the foreground (such as the phone number is used up), the system will automatically restore ActivityB, then, the global variables will naturally disappear.

Some people say that I don't need to use static variables to store global parameters with Application attributes. Can this problem be avoided? In fact, this is not the case because the Application object is destroyed and rebuilt after the process is killed and restored. Android does not guarantee that a notification will be sent to the program before the KILL process, therefore, we cannot save and restore global variables in the Application.

In addition, global variables cannot record classes (such as Activity and View) related to the Context of the android interface, because Android automatically manages these classes, record them to cause memory leakage issues that increase the reference count and cannot be released. If you must record them, use the weak reference WeakReference.

In general, parameter passing using global variables is not recommended in Android development. The best way is to completely use Intent to pass parameters according to Android development specifications, because the system automatically saves the Activity stack before the KILL process and the relevant Intent parameters, and automatic recovery. If you want to use a global variable, you must at least judge whether it is null when reading the global variable for processing, to avoid program errors and crashes. At the same time, it is best to save the data in onPause so that it can be restored after being killed.

However, I don't think global variables are useless and completely useless. They mainly need to understand and avoid the changes of global variables in the lifecycle of the android process. For example, you can use global variables to record your own global processing classes (such as factory classes and class registrars). You only need to pay attention to the recovery after the process is killed.

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.