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.