Application可以用于保存一些全局的变量,用于任意一个页面调用。但是存在一个问题,当程序打开并长时间不进行操作的话,程序进程有可能会被回收,相应的Application也会被回收,只是当我们在此操作时,系统会帮我们重建一个Application,但是其中保存的变量的值已经不存在了。所以在Application中保存数据也不少一定安全的,保险起见,在获得Application中变量的值的时候,自己进行一下处理。例如:
public String getUrlAddress() {
if(urlAddress==null){
SharedPreferences preferences = getSharedPreferences(SystemConsts.sharedPreferencesKey,MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
urlAddress = preferences.getString(SystemConsts.urlAddress, "");
}
return urlAddress;
}