Maintaining global Application state
As a possible solutions mentioned in previous article Leaving an Android application the Application object can come handy. If you want to store data, global variables that needs to be accessed from everywhere in the application, from multiple Activities, in other words is you want to maintain a global "state" of the whole application the Application object can help.
For this we must make a class which extends the Android.app.Application class add our own methods to it, and define this class in the AndroidManifest.xml as below:

- An example for the Application class:
- public class HelloApplication extends Application {
- private int globalVariable=1;
- public int getGlobalVariable() {
- return globalVariable;
- }
- public void setGlobalVariable(int globalVariable) {
- this.globalVariable = globalVariable;
- }
- @Override
- public void onCreate() {
- //reinitialize variable
- }
- }
After this in any of the Activities you can read and write the global variable like this:
- ((HelloApplication)getApplication()).setGlobalVariable(10);
- int valiable=((HelloApplication)getApplication()).getGlobalVariable();
The Application object is not destroyed until there are any undestroyed Activity in the application. Even when the whole application is wiped from memory you can reinitialize the variable in the onCreate method if needed.
You can try the same data storing, for example with a simple object with static field and methods, like below, it is a less elegant method, and if the reinitialization is needed after the whole application is killed you must implement the reinitialization in each activity.
- public class DataStoreClass {
- private static int globalVariable=1;
- public static int getGlobalVariable() {
- return globalVariable;
- }
- public static void setGlobalVariable(int newGlobalVariable) {
- globalVariable = newGlobalVariable;
- }
- }
To test how these objects store and reload values when the application is killed try the following:
- leave one of the applications activities with home button
- kill the application with a "task killer" application (you can find a lot on the market)
- by longpressing home button return to the killed application
Normally only the activity you last seen is restored this way. So if you want to restore some application level state, implement the application object's onCreate method, which is called even before the activity.
See Android Application class for more.
New tutorials from Helloandroid
Recent Apps
Android on Twitter
-
@Idevicegazette (iDevice Gazette)
GSM-to-Skype bridge lets you lose those roaming fees http://bit.ly/lbRJeh #android
11 years 45 weeks ago -
@tommy_banane (tom b.)
RT @AndroidFavorite: #Android New Desktop Android Market Is Live, Adds Several New Features http://zorr0.nl/lFwXNz
11 years 45 weeks ago -
@dwilliams5 (Dennis Williams)
just completed a runtastic run of 3.02 km in 40 min 11 s with #runtastic #Android App: http://tinyurl.com/5tvrpe3
11 years 45 weeks ago -
@S_Pinz (Spinz!)
RT @Androidheadline: Out of box #LG Optimus 3D got Quadrant 2420 score. Thanks @blink_c #io2011 #android http://twitpic.com/4whkdz
11 years 45 weeks ago -
@tayaitapps (Taya IT)
Next Google TV Looks A Lot Like Android http://t.co/dvlTim3 via @alleyinsider #google #apple #android #tv #honeycomb
11 years 45 weeks ago
Poll
Useful resources
Android Development Projects
- iOS/Android Developer to take older Games and bring them Current
- Android apps developer - need to finish urgent.
- Buliding MobileApp For onlie order
- looking for android APP developers
- Create an ecommerce app
- text-to voice for smartphones IOS - GOOGLE - HARMONY - AND ALEXA
- Optimize Images on App
- Create small feature with drag-drop text for Android
- Scouting for advanced website and Mobile apps developers. Potential Long-term contract.
- BLACK SCREEN