-2

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/

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
amuttsch
  • 1,254
  • 14
  • 24

3 Answers3

0

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.

public class App extends Application {
    public static ClementineConnection mClementineConnection = null;
    public static Clementine mClementine = null;

And in each of the activities:

App app = ((App)this.getApplication());

Also, you have to declare it in your manifest;

<application android:name="de.qspool.clementineremote.App" ...></application>

Look at those answers on the subject:

Community
  • 1
  • 1
RMalke
  • 4,048
  • 29
  • 42
  • Is it ok to just access the members with App.mClementine? Or do i have to use getApplication()? – amuttsch Feb 07 '13 at 11:40
  • Not sure about it. I used it on my code, but don't have it right now. – RMalke Feb 07 '13 at 11:43
  • I think your solution works. Have to do some more testing though. Thanks. – amuttsch Feb 07 '13 at 14:29
  • This can still cause NullPointerException. Refer these links: http://www.developerphil.com/dont-store-data-in-the-application-object/ and http://prismatic.github.io/android-state-saving/ – siddhant Dec 26 '15 at 06:45
0

Using static variables to store some lifecycle state of the app is not reliable. Even if you use a service, the app can be killed anytime in case of memory pressure. On latest Android versions, it would even be killed when the user slides the app away in the list of opened apps.

When an app is killed, the Linux process is killed, which means all activities, services are killed. Of course this implies that static variables are lost.

I fought a lot with this kind of things and found out that a good reliable way to keep app state alive all the time is to use SharedPreferences instead of static variables to store app's state variable.

gpo
  • 3,388
  • 3
  • 31
  • 53
  • The objects stores information, that doesn't need to be available after the app closes. It stores information like the current track, metadata etc. that is downloaded from the server each time the app connects to it. So it needn't be stored in SharedPreferences. (I didn't vote down your post) – amuttsch Feb 07 '13 at 14:28
-2

You should save your static data in the application by extending the Application Class always:

public class App extends Application {
    public static ClementineConnection mClementineConnection;
    public static Clementine mClementine;
}

It will be automatically instantiated when your application is started so just save the in it's variables in your oncreate of first activiy as follows-

App.mClementineConnection = data;

And It wont make any null pointer exception till your application is closed.

Thanks, enjoy my friend.

Shridutt Kothari
  • 7,326
  • 3
  • 41
  • 61