Note that there are some explanatory texts on larger screens.

plurals
  1. POStatic data saved inside singleton is null sometimes when returning to app from background
    text
    copied!<p>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. </p> <p>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.</p> <p>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. </p> <p>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 . </p> <p>Following my search I found a great and simple explanation, explaining when/why a static data can become null - <a href="https://stackoverflow.com/questions/1944369/android-static-object-lifecycle-application-act-crazy">Android static object lifecycle</a> @Samuh answer and I quote</p> <p>"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 " </p> <p>About #1 and #2 - I don't really know when these occur. </p> <p>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 ? </p> <p>Hope someone can help , thanks in advance. </p> <p>My singleton class:</p> <pre><code> 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; } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload