Less static variables in Android (Static life cycle for Android)

Source: Internet
Author: User

In Android, use less static variables.

I am now doing an application in which the previous developer used static variables to store the cookie, a global static variable used to verify the identity.

At this point the customer response, the application for a long time not used, re-use, will prompt identity expired.

Later, the problem is basically determined on the static variable.

StackOverflow checked the life cycle of the static variable in Android, someone said

Lifetime of a static variable:a static variable comes into existence when a class was loaded by the JVM and dies when the Class is Unloaded,if-Create an Android application and initialize a static variable, it'll remain in the JVM until O NE of the following happens:

1. The class is unloaded
2. The JVM shuts down
3. The process dies

What happens in our application should be that the process is killed by the system.

Later, the situation was discovered, is to constantly open the application, when the system memory is not enough, the application process will be killed. When you open the app again, an identity expiration occurs, which is the case where the static variable is empty

Static variables, to use with caution!

Less static variables in Android (Static life cycle for Android)

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.