I have a simple class to keep a few variables to share them between activities in my game, something like:
public class TheGlobals {
public static boolean IsFullGame = false;
}
Now, when the game launches, the initial activity is MainMenu, which determines whether the user has purchased the full game, and sets the variable accordingly. For example, in case the user bought the game, it would do
TheGlobals.IsFullGame = true;
So far so good. From here, the user clicks Play and switches to the Game activity where the main game action happens. In this second (Game) activity, there are a few places that I check whether it's the full / purchased game by accessing that global static variable, and enable or disable certain features accordingly.
Now, the user plays and at some point hits the Home button or switches to other apps. After some time, the user comes back to my game, launching it from the recent apps, which opens the game where they left off, that is directly in the second (Game) activity, and the user happily continues playing.
Am I correct to assume that at this point, the value of my global static variable can be either True or False, depending whether the process was destroyed or not, or is there any guarantee that Android will restore its value. I am thinking that if the process was kept alive, then the value will remain True (as it was set in the first activity) and got preserved as long as the process was kept alive; or if the process was destroyed, and the user comes back to it later, and opens my game directly in the second (Game) activity, then the global static variable's value in that case would default to False (just like it's defined on the class level, and without a chance of being set in the first activity).
Thank you for any comments.
The space for the static variable is allocated only one time and this is used for the entirety of the program. Once this variable is declared, it exists till the program executes. So, the lifetime of a static variable is the lifetime of the program.
Static variables are also known as Class variables which are declared with the “static” keyword in a class. A single copy of each variable per class is to be shared by all instances of the class. Static variables are stored in static memory.
The static variable can be used to refer to the common property of all objects (which is not unique for each object), for example, the company name of employees, college name of students, etc. The static variable gets memory only once in the class area at the time of class loading.
Am I correct to assume that at this point, the value of my global static variable can be either True or False, depending whether the process was destroyed or not, or is there any guarantee that Android will restore its value. I am thinking that if the process was kept alive, then the value will remain True (as it was set in the first activity) and got preserved as long as the process was kept alive; or if the process was destroyed, and the user comes back to it later, and opens my game directly in the second (Game) activity, then the global static variable's value in that case would default to False (just like it's defined on the class level, and without a chance of being set in the first activity).
Yes, You are correct. At this point process is Android Application Process.
Still your application process running it has value persistent for your variable TheGlobals.IsFullGame
.
Now, the user plays and at some point hits the Home button or switches to other apps. After some time, the user comes back to my game, launching it from the recent apps, which opens the game where they left off, that is directly in the second (Game) activity, and the user happily continues playing.
Because your application available in background task means Application process is running so you are getting correct values.
Now What about,
Simply Android system kill your application process to remain other application running state, and you are getting default value of your variable no state preserved
Simply on starting of device, user have to start your application from begin, and no persistent state for your variable, it has default value only.
So in this scenarios your application persist variable value till your application running and it has allocated memory space on system (application running space on device RAM)
Solution:
You have to persist values of your application variables until life time of application means till application not un-installed from device, so better to use SharedPreference to store your variable value which remain persistence until you change thru the application.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With