Android全局变量如何创建全局变量,保持在应用程序生命周期周围的保留值,而不管哪个活动正在运行。
3 回答
阿晨1998
TA贡献2037条经验 获得超6个赞
android.app.Application
public class MyApplication extends Application {
private String someVariable;
public String getSomeVariable() {
return someVariable;
}
public void setSomeVariable(String someVariable) {
this.someVariable = someVariable;
}}android:name=".MyApplication"
<application android:name=".MyApplication" android:icon="@drawable/icon" android:label="@string/app_name">
// set((MyApplication) this.getApplication()).setSomeVariable("foo");// getString s = ((MyApplication) this.getApplication()).getSomeVariable();
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
Singleton Pattern
package com.ramps;public class MyProperties {private static MyProperties mInstance= null;public int someValueIWantToKeep;
protected MyProperties(){}public static synchronized MyProperties getInstance() {
if(null == mInstance){
mInstance = new MyProperties();
}
return mInstance;
}}MyProperties.getInstance().someValueIWantToKeep
- 3 回答
- 0 关注
- 735 浏览
添加回答
举报
0/150
提交
取消
