Maintaining global Application state


SDK Version: 
M3

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:

  1. An example for the Application class:
  2.  
  3. public class HelloApplication extends Application {
  4.         private int globalVariable=1;
  5.  
  6.         public int getGlobalVariable() {
  7.                 return globalVariable;
  8.         }
  9.  
  10.         public void setGlobalVariable(int globalVariable) {
  11.                 this.globalVariable = globalVariable;
  12.         }
  13.         @Override
  14.         public void onCreate() {
  15.                 //reinitialize variable
  16.         }
  17. }

After this in any of the Activities you can read and write the global variable like this:

  1. ((HelloApplication)getApplication()).setGlobalVariable(10);
  2. 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.

  1. public class DataStoreClass {
  2.         private static int globalVariable=1;
  3.  
  4.         public static int getGlobalVariable() {
  5.                 return globalVariable;
  6.         }
  7.  
  8.         public static void setGlobalVariable(int newGlobalVariable) {
  9.                 globalVariable = newGlobalVariable;
  10.         }
  11. }

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.

Comments

Ya i have the same question.. would this work if other services are running...
Mediafire Video Song Download
Rugby World Cup 2011 Live Streaming

Business
I appreciate the project that you have been achieved on the time.
I think this idea is better for the future also.

توبيكات نونو
توبيكات
توبيكات سعودي

توبيكات 2012

توبيكات بنات

العاب نونو

العاب

العاب بنات

games

شات لمني الصوتي

دردشة لمني الصوتية

شات صوتي لمني

شات صوتي

دردشة صوتية

دردشة

دردشه

شات سعودي

شات خليجي
سكر بنات

جات

شات صوتي سعودي خليجي

chat voice

ahj

خليجي الصوتي

سعودي الصوتي

دردشة صوتي

شات صوتي
دردشة صوتية
شات كتابي

شات كتابي خليجي

شات عسل الصوتي

دردشة كتابية

chat
سعودي كول
سعودي كول 6666

كول

سعودي

سعودي كول انحراف

سعودي كول بنات

سعودي كول 1994

chat saudi col ‏

شات سعودي كول

سعودي انحراف

سعودي انحراف2010

سعودي انحراف الصوتي

شات سعودي انحراف

دردشة سعودي انحراف

سعودي انحراف الصوتية

شبكة سعودي انحراف

سعودي انحراف الاصلي

سعودي انحراف كول

سعودي انحراف 2010

انحراف سعودي

saudideviation

دردشة صوتية سعوديه
دردشة صوتية سعودية

دردشة كتابية
دردشة كتابية خليجية
شات
دردشة
خاص للبنات

عرب ذوق

عرب ذوق الصوتي

عرب ذوق الصوتية

دردشة عرب ذوق

شات عرب ذوق

شبكة عرب ذوق

شات صوتي بنات

شات بنات الصوتي

دردشة بنات الصوتي

Girls Chat

شبكة عفناك

صوتية عفناك

شات عفناك

دردشة عفناك

عفناك الصوتي

دردشة عفناك

الخيال
الخيال كام
شبكة الخيال
الخيال الصوتي
الخيال الصوتية
دردشة الخيال
الخيال الصوتية
دردشة صوتية الخيال

شات سعودي خليجي

منتدى نونو

منتدى

منتديات

موقع

شبكة

نونو

Chat Nono

ahj w,jd

]v]am w,jdm

دليل مواقع ويب

دليل مواقع

دليل

مواقع

بنت كول
بنت كول الصوتي
شات بنت كول

دردشة بنت كول
شات بنت كول الصوتية

بنت كول الصوتيه
سعودي كول
صوتية سعودي كول
شات سعودي كول
دردشة سعودي كول
سعودي كول الصوتي
سعودي كول 6666
سعودي كول6666
سكر بنات
شات صوتي زين
شات صوتي ملوك
شات صوتي سعودي
شاتات صوتيه
مكتبة ماسنجر
شات صوتي حبي
شات صوتي كويت
YouTube - Broadcast Yourself.‏ , اليوتيوب نونو
صيف كام
شات صوتي كول
شات انحراف
وه بس
خريطة الموقع نونو
الرياض كول الصوتي
كامات 6666
شات المها
كامات6666
شات كامات 6666
كامات 666
كامات 66
سعودي انحراف
شاتكامات6666
سعودي احوه
شات سعودي احوه
سعودي احوه الصوتي
سعودي احوه كول
دردشة سعودي احوه
احوه سعودي
بنات احوه
دبي الصوتي
سعودي في اي بي الصوتي
شبكة الرياض الصوتي
روعة الليل
لايف كام
الخليج كام
شات كان زمان الصوتي
شات صوتي قصيمي
شات قلبي
ارجوان
شات صوتي قطري
بدور الخليج

منتدى روح

شبكة روح

روح ديزاين

تحميل ماسنجر بلس

توبيكات حزينه

توبيكات

ماسنجر

ماسنجر بلس

تحميل ماسنجر

توبيكات رومنسيه

منتديات روح
دردشة
شات سعودي
خليجي
شات صوتي
توبيك
موقع توبيكات
سعودي كول 6666
سعودي انحراف 2011

سعودي كول
سعودي انحراف

Really Really Thanks For This Wonderful article. Its glad that nowadays also, there is some original content on the internet. Otherwise, everywhere you go you will find duplicate content. Thanks Again. Like to Read You article and blog.

movie download | mediafire links | all buzzy news | ready movie download

I was very pleased to find this site.I wanted to thank you for this great read.
windows vps | forex vps

it is sometimes hard to Pandora in a world of adults Pandora Bracelets

Would it be possible to open a tcp socket in the application class? Then I can access it from any of my activities.
I need to keep a tcp connection open all the time even when the application is minized. Should the user return within 5minutes the connection should still be alive.

Or maybe start a service in the application? Not sure...
Basically, what I need is an app, in which I keep a tcp connection to the server open, simply reading for xml messages. If it finds a message it simply puts in into a buffer. If not used for 5 minutes, kill the connection and app if any.
The app will read messages from the buffer. If it sees the socket is closed due to timeout it kills the application.

Any help would be appreciated.

Would this work if you have services running in another process?

tito

I didn't know you could do that. Good post.
I've always used SharedPreferences for global variables.
The difference seems to be that SharedPreferences are persistent after your application ends. I assume SharedPreferences is also slower.

Would this work if you have services running in another process?