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
-
@StephanieNich10 (Stephanie Nichols)#android I laughed so hard at ochocinco 's avi. Hahahaha http://t.co/MPuhhi3m
25 weeks 4 days ago -
@CarlaAtkins8 (Carla Atkins)#android Omg! This is actually f'n interesting http://t.co/JodMOehr
25 weeks 4 days ago -
@MarianMcleod12 (Marian Mcleod)#android Precisely what song is? http://t.co/YmJXU0rB
25 weeks 4 days ago -
@JoBeach15 (Jo Beach)#android haha this made me laugh, i love ted:-) http://t.co/gtcWQ79C
25 weeks 4 days ago -
@aochart3 (青ちゃ)Start playing Paradise Island on Android http://t.co/DEID0Ao5 #Android #Androidgames #Gameinsight http://t.co/e1bifSeL
25 weeks 4 days ago
Poll
Useful resources
Android Development Projects
- Android bases applications development (only indian & very skilled developers apply) by sudhirjeet01
- Health Care App - want freelancer in Georgia by rshearer
- IOS+Android+HTML5+PHP Expert Needed Urgently by johnusa1
- NFC Keyboard Wedge by billinginfoes
- I need an iphone and android app + web services by senjy
- Mobile Remote control software for android only by leoagent786
- Add a "Print" button, with code, to our existing process (PDF explains) by sportingchance
- I need a phone app - Android and Iphone - a copy of an existing app by DooniseP
- Apple & Android app - Connecting our CRM and an external GPS device by kshyamnatraj
- Flash&Actionscript app for android by mobistar2013



