Hi What is the best way to store global objects/instances through out the application ?
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With