Does android save static variables?

I am writing a simple Android application, which is basically a modification of the fragment demo available in the Android documentation. The application has a file called Ipsum.java, which has a static ArrayList of strings called headers.

In the onCreate () method of the main action, I have the following code that adds some elements to the list of arrays.

if (savedInstanceState == null){ Ipsum.Headlines.add("String 1 "); Ipsum.Headlines.add("String 2"); } 

savedInstanceState is the set that the system passes to the method if the application resumes from some inactive state. The logic is that if savedInstanceState is NULL, then the application does not resume, but starts as a new instance.

If I leave the application using the Home button and run the application again, the List array contains only two elements: "String 1" and "String 2". (This is the desired behavior)

However, if I leave the application using the back button, and then enter the application again, the elements "String 1" and "String 2" will be added again . Then the array has 4 elements.

 String 1 String 2 String 1 String 2 

(The contents of arrayList can be seen as they are used to populate the listView) It seems that the application stores the contents of the list of static arrays when the "Back" button is pressed .. and that the package is not passed to the onCreate () method when the application restarts. Can someone explain what is happening here in terms of the application life cycle?

+4
source share
3 answers

May This will help you:

Let's start with a small background: What happens when the application starts?

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

Static variable lifetime: A static variable occurs when the 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 events occurs:
1. class is unloaded
2. JVM shuts down
3. the process is dying

Please note that the value of the static variable will be saved when switching to another activity of another application, and none of the three above will be executed. If any of the above three events statically loses its value.

Read more: Read Samuh's answer in this link ... Click here p>

+10
source

Your activity resumes. If you want to control what happens, do onResume() .

For more information, see Activity Lifecycle Management .

EDIT:

Static variables are a Java concept. static means that for the whole class there is only one copy of the variable. An alternative is that each object will have its own copy.

So, while your code is running, you only have one copy of the variable in your program. It is not saved anywhere unless you add code for this.

+2
source

Static variables are associated with the class, and they will live as long as the class is in memory, and will be destroyed when the class is unloaded (which is very rare). It can happen when -

  -You force stop your app. -Application crashes. -You clear your app data. -Switch off your Device(Shutdown DVM). 
0
source

Source: https://habr.com/ru/post/1495254/


All Articles