How can I create global variable keep remain values around the life cycle of the application regardless which activity running.
Use SharedPreferences to store and retrieve global variables.
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this);
String userid = preferences.getString("userid", null);
Easy!!!!
Those variable you wanna access as global variable, you can declare them as static variables. And now, you can access those variables by using
classname.variablename;
public class MyProperties {
private static MyProperties mInstance= null;
static int someValueIWantToKeep;
protected MyProperties(){}
public static synchronized MyProperties getInstance(){
if(null == mInstance){
mInstance = new MyProperties();
}
return mInstance;
}
}
MyProperites.someValueIWantToKeep;
Thats It! ;)