android使用全局变量
2010-11-17 10:50
399 查看
一开始我是用static来做全局,后来在网上看见老外有一个很好的办法
public class MyApp extends Application {
private Integer[] state;
public Integer[] getState() {
return state;
}
public void setState(Integer[] state) {
this.state = state;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setState(we);
}//一定要在onCreate方法里给变量赋值
public Integer[] we=new Integer[]{
R.drawable.b_0,R.drawable.b_1,R.drawable.b_2,
R.drawable.b_3,R.drawable.b_4,R.drawable.b_5
};
}
在所需要的activity中使用
MyApp appState = (MyApp)getApplicationContext();
Integer[] we = appState.getState();
来获取,最后一步别忘了在manifest中的application标签中添加 android:name=".MyApp"(这儿不是新建<application></application>)
public class MyApp extends Application {
private Integer[] state;
public Integer[] getState() {
return state;
}
public void setState(Integer[] state) {
this.state = state;
}
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
setState(we);
}//一定要在onCreate方法里给变量赋值
public Integer[] we=new Integer[]{
R.drawable.b_0,R.drawable.b_1,R.drawable.b_2,
R.drawable.b_3,R.drawable.b_4,R.drawable.b_5
};
}
在所需要的activity中使用
MyApp appState = (MyApp)getApplicationContext();
Integer[] we = appState.getState();
来获取,最后一步别忘了在manifest中的application标签中添加 android:name=".MyApp"(这儿不是新建<application></application>)
相关文章推荐
- Android全局变量使用
- Android 全局变量的使用
- 4.1.2 Android 通过Applicatoin 来设置全局变量和使用
- 为何Android开发中不推荐使用全局变量传参
- Android中用Application类实现全局数据变量的使用
- android 使用Application保存全局变量
- Android用类继承Application的全局变量使用注意
- [转]android使用全局变量的两种方法
- android 全局变量的使用
- Android 使用全局变量
- Android全局变量的定义与使用
- Android 使用全局变量
- Android全局变量使用
- android使用全局变量的两种方法
- android使用全局变量的两种方法
- Android用Application设置全局变量以及使用
- Android入门篇五:使用全局变量在Activity之间传递数据
- android使用application保存全局变量
- Android用Application设置全局变量以及使用
- Android用Application设置全局变量以及使用