Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Store application global data

Tags:

Hi What is the best way to store global objects/instances through out the application ?

like image 843
Harikrishnan R Avatar asked Apr 20 '11 07:04

Harikrishnan R


1 Answers

Here is the following code which I use to store String in Application Context.

I make the class GlobalVariable.java

import android.app.Application;  public class GlobalVariable extends Application  {       private String myState;        public String getState()       {         return myState;       }//End method        public void setState(String s)       {         myState = s;       }//End method }//End Class 

In .Manifest I add the following code

<application  android:icon="@drawable/icon" android:label="@string/app_name" android:name="GlobalVariable"> 

Where I want to set the value of string I use the following code

GlobalVariable appState = ((GlobalVariable)getApplicationContext()); appState.setState("Testing"); 

& where I want to Retrive the data I use

GlobalVariable appState = ((GlobalVariable)getApplicationContext()); appState.getState(); 
like image 98
Siddiqui Avatar answered Sep 23 '22 21:09

Siddiqui