Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static data saved inside singleton is null sometimes when returning to app from background

I use a classic singleton pattern to store data I get from webServices inside my initial activity (splash activity) and then access it throughout the application.

I have a splash activity which leads to MainActivity which is the sliding menu activity with fragment inside it , the initial fragment is a fragment that contains support map fragment.

my issue is with the data stored inside the singleton , sometimes when the app goes to the background (pressing home and moving to another app) and then go recent apps and choose my application , the singleton's data is null which crashes my app.

I have spent hours checking if my singleton is written as should and what are the possible reasons for static data to become null if I didn't make it null myself .

Following my search I found a great and simple explanation, explaining when/why a static data can become null - Android static object lifecycle @Samuh answer and I quote

"So if you create an android application and initialize a static variable, it will remain in the JVM until one of the following happens: 1. the class is unloaded 2. the JVM shuts down 3. the process dies "

About #1 and #2 - I don't really know when these occur.

Regarding #3 - this is perfectly fine as if user will click my app from "recent applications" this will invoke the launcher activity in my case "splash activity" and this will get the data (up to date) from servers and store it again inside my singleton - perfect! , that's exactly what happens inside my other applications , the difference is I'm not using a sliding menu and fragments , only activities. Here in my case returning from background will not invoke the launcher Activity which is the "splash screen" but will invoke the MainActivity (the main sliding menu activity) along with the initial fragment , and when I try to access data from my singleton all data inside it is null, the singleton itself is not null because at the onCreate() of the fragment i use getInstance().I believe that this implies that the singleton was lost and now recreated with no data inside it , so if the process is killed why am I not returned to the launcher activity ?

Hope someone can help , thanks in advance.

My singleton class:

 public class MySingleton {
  // some private data members with getters/setters
  private String x,y,z;

      // the instance
  private static MySingleton theInstance = null;

      //private ctor 
      private MySingleton(){
          //doing something 
      }

      //get instance
      public static synchronized MySingleton getInstance() {

     if (theInstance == null)
        theInstance = new MySingleton ();
     return theInstance;
   }
 }
like image 275
EviatarS Avatar asked Nov 21 '13 12:11

EviatarS


People also ask

Can a singleton be null?

the instance on a Singleton could never become null, except for when the app would be completely restarted (in which case it should by recreated by my application class).

Why instance is static in Singleton?

It is static so every instance of the Singleton type will use the same variable, hence the "singleton" pattern. Save this answer. Show activity on this post. Succinctly, Singleton is a design pattern used to ensure that only one instance of something is ever created within a given scope.

How do you ensure only one instance in a singleton class?

1)Private constructor to restrict instantiation of the class from other classes. 2)Private static variable of the same class that is the only instance of the class. 3)Public static method that returns the instance of the class, this is the global access point for outer world to get the instance of the singleton class.

Can Singleton be destroyed?

The true, non monobehaviour singleton can't really be destroyed or reinitialized as the static field is read only. If you need this you have to do the usual singleton approach with a normal private static variable. To force a reinitialization you just add a method "Destroy" which sets the static reference to "null".


1 Answers

Your singleton is killed by the system, generally because it needs memory, or because the process is maybe too old.

What I suggest you is:

-Save data from your singleton, in shared prefs for example, so it can be easily recreated if needed. It generally works fine -Save your variables by overriding OnSaveInstanceState in your Activity, and restore them when it is recreated.

like image 106
Aerilys Avatar answered Oct 18 '22 13:10

Aerilys