Issue with global variables
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): stopNavigati开发者_运维百科ng
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
1- How can this be possible?
Your process may be terminated at any point. You cannot assume how long any static data members or custom Application
objects will live. They should only be used as a cache, at best.
2- How can I force my app to exit when going to background?
You don't.
I know I'm not supposed to do it
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.
but I can't find other solution.
Initialize your data. If your application is crashing because a new custom Application
instance was created, your Application
subclass has bugs, which you need to fix.
You should use the appropriate life-cycle event handlers to save data and restore it upon re-activation: onSaveInstanceState(), onPause(), and onResume(). You cannot safely assume that your global fields will remain intact.
I would suggest familizarizing yourself with the Activity life-cycle.
Its simple, you must initialize your declared variables in Application.OnCreate, I have just tested and it works, when the application crash it calls Application.OnCreate again.
I wish that helped.
@Override onPause
in your activity and then use finish();
The Application
class is for initializing global variables - so you have this part correct. However, you will have to declare these variables in another class, since the Application
only is called when the app launches anew - not when it is in the background.
You should also make these initializations in the Application
's onCreate()
method (using the @Override
annotation).
I ran across a similar problem and basically this article explains it well http://www.developerphil.com/dont-store-data-in-the-application-object/
This being the key paragraph and what is happening to you:
Which brings us to the core of the problem: The application object will not stay in memory forever, it will get killed. Contrary to popular belief, the app won’t be restarted from scratch. Android will create a new Application object and start the activity where the user was before to give the illusion that the application was never killed in the first place.
I know this post is old but thought that this might be helpful for future users.
精彩评论