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
-
@800whitet (justin mayfield)I've just received an achievement: Terror of untidiness https://t.co/oO4KPwsg #Android #Androidgames
12 min 35 sec ago -
@Gleydsonxdd (Gleydson )I have just connected Textile Mill in My RailWay. You can get it for FREE on Android! http://t.co/MhK6XUog #android #androidgames
12 min 41 sec ago -
@hong1515 (Harry Seunghwan Hong)HP TouchPad Android kernel now in the hands of CyanogenMod team http://t.co/6FV1dxYD #android
12 min 44 sec ago -
@eddieiris67 (ed oneill)I've just received an achievement: Total Upgrade http://t.co/e7WfiRnn #Android #Androidgames
12 min 52 sec ago -
@kmj22120 (김민정)I've just received an achievement: Income Collector https://t.co/0ejBO0sY #Android #Androidgames
12 min 56 sec ago
Poll
Useful resources
Android Development Projects
- Android App wanted immediately by JoePublic
- LIST DATA PROJ by nhammoud
- Nonpublic project #1433932 by subpariq
- Alarm Android Application Design by globalheed
- Simple Album App for Android by ayfonfan
- iOS and Android photo manipulation 'Morph App' by whatwedomedia
- Onsite Software Engineers in Germany by sudhirshree
- Augmented reality by merder99
- Nonpublic project #1433560 by vobla73
- Mobile app coder needed for quick, simple app by Ergometrix




Comments
دردشة سورية دردشة
دردشة سورية
دردشة لبنانية
دردشة عراقية
شات سوري
شات لبناني
دردشة سوريا
دردشة لبنان
شات سوريا
شات لبنان
دردشة السويدي
منتديات السويدي
اغاني عراقية
صور فنانين
الرياضة العراقية
شعراء العراق
نغمات عراقية
اغاني عربية
اغاني كردية
دردشة عراقية
دردشة بنات العراق
دردشة صبايا بغداد
دردشة البصره
دردشة بغداد
دردشة بغدادية
دردشة صبايا بغداد
دردشة شباب العراق
دردشة بنات العراق
دردشة الكرادة
دردشة دمشق
دردشة بيروت
دردشة حلب
دردشة حلب
دردشة عراقية
دردشة العراق
شات عراقي
جات عراقي
دردشه عراقيه
دردشة صبايا لبنان
دردشة بنات لبنان
شات صوتي | دردشة صوتية | كلام
شات صوتي
| دردشة صوتية
|
كلام
| شات كلام
|
دردشة كلام
| دردشة صوتية
|
شات صوتي
| شات
|
Chat Voice
| ahj w,jd
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
دردشه
| دردشة
|
صوتي
| صوتية
|
شات صوتي
| دردشة صوتية
|
شات صوتي
| دردشة صوتية
|
الكلام
| دردشه صوتيه
|
]v]am w,jdm
| ]v]ai w,jdi
شات صوتي
شات صوتي
شات صوتي
Good information
Mediafire Video Song Download
Rugby World Cup 2011 Live Streaming
What type of services ?
Ya i have the same question.. would this work if other services are running...
Mediafire Video Song Download
Rugby World Cup 2011 Live Streaming
Thank you - the site very
Thank you - the site very very nice
شات الحب - شات مصري - شات بنات مصر - شات صوتي - دردشة مصرية - شات مصرية - شات حب - شات - دردشة - Chat - love chat - دردشة الحب
Best format
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
سعودي كول
سعودي انحراف
thanks
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
Hosting
I was very pleased to find this site.I wanted to thank you for this great read.
windows vps | forex vps
Thank you for
المنتدي العام
المنتدى الادبى
المنتدى الطبى
حواديت بنات
المنتدى السياسى
منتدى الاذكياء
تاريخ الدول والاثار
اخبار النجون والفانيين
قسم الاغانى الفردية singles
الاغاني الاجنبية
الاغاني الشعبى
الريمكسات
الفيديو كليبات
كلمات الاغانى
التهانى والاهدءات
منتديات الحب والرومانسية
التعارف والصداقة
حلال مشاكل الحب
الضحك والفرفشة
العاب الكمبيوتر
العاب كتابية
الرياضة العالمية
الدورى المصرى
عالم السيارات
منتديات الصور
منتديات البرامج
منتديات الموبايل
برامج ومسلسلات رمضان 2010
افلام عربى
افلام اجنبى
مسلسلات عربي واجنبى
افلام هندى
مسرحيات
افلام انيماشن وكارتون
اعلانات الافلام
ترجمة الافلام
الفوركس
قسم البنوك الالكترونية
الشركات الربحية المجانية
قسم الربح من موقعك
قسم الكتب والبرامج المالية والادارية
دليل الدردشات المصرية والعربية
it is sometimes hard to
it is sometimes hard to Pandora in a world of adults Pandora Bracelets
TCP Connection
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.
thank you
Would this work if you have services running in another process?
tito
SharedPreferences
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.
Services?
Would this work if you have services running in another process?