Does android save static variables?

折月煮酒 提交于 2019-12-05 09:13:11
Bhavin Nattar

May This Help you:

Lets start with a bit of background: What happens when you start an application?

The OS starts a process and assigns it a unique process id and allocates a process table.A process start an instance of DVM(Dalvik VM); Each application runs inside a DVM. A DVM manages class loading unloading, instance lifecycle, GC etc.

Lifetime of a static variable: A static variable comes into existence when a class is loaded by the JVM and dies when the class is unloaded.

So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens:
1. the class is unloaded
2. the JVM shuts down
3. the process dies

Note that the value of the static variable will persist when you switch to a different activity of another application and none of the above three happens. Should any of the above three happen the static will lose its value.

For More Detail: Read the Answer of Samuh in this Link... Click Here

Your activity is being resumed. If you want to control what happens, implement onResume().

See Managing the Activity Lifecycle for details.

EDIT:

Static variables are a Java concept. static just means that there is only one copy of the variable for the whole class. The alternative is that each object would have it's own copy.

So while your code is running, you just have one copy of the variable in your program. It doesn't get saved anyplace, unless you add code to do that.

Static variables are associated with a class and they will live as long as the class is in the memory,and destroy when class gets unloaded (which very rarely happens). It can happen when-

 -You force stop your app.
 -Application crashes.
 -You clear your app data.
 -Switch off your Device(Shutdown DVM).
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!