I defined some global variables in my app by extending Application, as shown below. When I leave the app, open more apps and play a bit with them, and go back to my app, the global variables have been deleted and my app crashes. I’ve got 2 questions:
1- How can this be possible?
2- How can I force my app to exit when going to background? I know I’m not supposed to do it, but I can’t find other solution…
Thanks
public class GlobalVars extends Application {
public static HashMap<Integer, String> ID2Cat = new HashMap<Integer, String>();
// User logged bool
public static boolean isLogged = false;
// Current menu item
public static int currentMenuItem = 0;
public static boolean isHome = false;
// Goodideas
public static JSONObject goodIdeas = new JSONObject();
// Meteo
public static JSONArray weatherItems = new JSONArray();
// More stuff
}
Logcat
Here’s what makes me think my app is killed. This is shown at some point while playing with other apps.
I/ActivityManager( 2465): Process com.mysite.myapp (pid 23538) has died.
I/WindowManager( 2465): WIN DEATH: Window{4852a678 com.mysite.myapp/com.mysite.myapp.Home paused=false}
I/WindowManager( 2465): WIN DEATH: Window{485b63a8 com.mysite.myapp/com.mysite.myapp.Home paused=false}
I/WindowManager( 2465): WIN DEATH: Window{4826fbf8 com.mysite.myapp/com.mysite.myapp.ItemList paused=false}
I/WindowManager( 2465): WIN DEATH: Window{48286f90 com.mysite.myapp/com.mysite.myapp.ItemDetail paused=false}
W/GpsLocationProvider( 2465): Unneeded remove listener for uid 1000
D/GpsLocationProvider( 2465): stopNavigating
D/gps_BRCM( 2465): [status check] on_stop() : GPS_STATUS_SESSION_END
D/gps_BRCM( 2465): gps_engine_status_update 2
D/GpsLocationProvider( 2465): send an intent to notify that the GPS has been enabled or disabled
D/gps_BRCM( 2465): gps_stop: called
V/GpsLocationProvider( 2465): hybridGpsSensorDeregister : No registered sensorManager
D/GpsLocationProvider( 2465): hybridGpsSensorDeregister
Your process may be terminated at any point. You cannot assume how long any static data members or custom
Applicationobjects will live. They should only be used as a cache, at best.You don’t.
Not only that, but it will not help you in this case. All it will do is mean that you crash 100% of the time, since you didn’t actually bother to initialize properly.
Initialize your data. If your application is crashing because a new custom
Applicationinstance was created, yourApplicationsubclass has bugs, which you need to fix.