Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are static variables really safe across activities within an application in android

Tags:

java

android

I have a multi activity application and save data in the main menu activity that is used by many of the other activities.

One of my variables in the Main activity might be this

static double targetAngle = 45;

I might call that variable from another activity like this

diff = Main.targetAngle - angle;

or I might set it like this

Main.targetAngle = angle;

From this reference, http://developer.android.com/guide/faq/framework.html This seems like a correct way to pass data. But there is always talk about activities being killed by the OS at any time.

My question is, is this safe or not?

As an alternative, I have at the suggestion of SO members, a Class called Helper that has some functions that are used in every activity which also have some static data. For example, the Helper Class has this data followed by my functions

public class Helper {
static double[] filter1 = new double[]{0,0,0,0,0};
static double[] filter2 = new double[]{0,0,0,0,0};
static double cog = 0;
    ...
   various functions....
}

I could save my shared variables in that helper class if that would be better. That class is called once a second and if it is ever killed, I am dead and really need to rethink things. I should mention that I have had no issues with what I am doing but one of my users is having his Nexus-7 crash and we don't know why so I was thinking he might have more applications running than I do, thus my question.

I should also mention that if the user exits the application, I have saved any variables that need to be saved in files on the SD card so they can be re-loaded. In other words, the loss of data when the application is killed is not an issue. My question is only if my main activity was killed when the application was still alive.

like image 704
Allen Edwards Avatar asked Nov 25 '13 00:11

Allen Edwards


1 Answers

My thanks to selbie and squonk for answers in the comments. Lacking an official answer from either I post my own as I want to close this out.

What I conclude is that per this post Using static variables in Android, the static variables themselves are not destroyed and what I am doing is safe.
This post, Clearing Static data onDestroy() states that "The value of static variables will persist as long as the class is loaded...The only reason ... that Android will unload a class is that your app gets completely removed from memory"

However, it may not be good practice as pointed out by squonk. Using a Class that is not an Activity to host static global variables and common functions may be better practice and is easier to maintain and generally cleaner. I will be moving in that direction as it has other advantages as well.

In either case, it is clear that when the application is destroyed, the variables will be re-initialized and needs to be reset manually. In my case, I store data on the SD card in files, which is one of several ways to save data.

I found the above links with a new Google search. Obviously I should have done a search with that wording earlier but none of my searches returned useful results, mostly finding the singleton vs extension of application debate.

like image 139
Allen Edwards Avatar answered Oct 11 '22 08:10

Allen Edwards