Global data with Application Context - Tuong-Nguyen/Android GitHub Wiki
Global data that are shared between objects (such as: Activity - BroadcastReceiver - Service ...) can be put in Application instance.
Define Application context
- Create a class which extends Appliation class
- Add properties into this class for storing shared data.
public class GlobaVariables extends Application{
private string text;
public string getText(){
return this.text;
}
public void setText(string text){
this.text = text;
}
}
Access application context
In classes where we want to access the shared data:
text = (GlobalVariables) getApplicationContext().getText();