For storing runtime data in my app I have a class with two static variables:
public class App {
public static ClementineConnection mClementineConnection = null;
public static Clementine mClementine = null;
}
Those are initialized in the onCreate Method of the first Activity. This Activity does nothing but starting other activities depending on a state (is the app connected to server).
If the App is idle or running in the background with a notification, sometimes a NullPointerException occurs in the other onCreate Methods when accessing one of the static variables. Somehow they get garbage collected.
The App has a Service with a Thread running in the Background, so I thought there must be a reference all the time.
Do you know how I can prevent those static variables from being garbage collected? While the app is connected to the server, the information must be available. Creating a new instance (eg. Clementine.getInstance()) is not an option.
If you want to have a look at the code: https://code.google.com/p/clementine-remote-android/source/browse/
What I think you need to do, is use Android Application Class.
There you can share and persist data to all of your activities, and initializes the variables there. You can make your App Class extend it.
And in each of the activities:
Also, you have to declare it in your manifest;
Look at those answers on the subject:
Using the Android Application class to persist data
Use Application class for global variables