woa,找到一个和我有类似需求的问题,其下给出了不错的解决方案,也正是我之前想到的,这种方法貌似很方便。
The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context.
--如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。
As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application.
--每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。
The way to do this is to create your own subclass of android.app.Application,and then specify that class in the application tag in your manifest.Now Android will automatically create an instance of that class andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect):
--方法是创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。
给个例子:
class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
This has essentially the same effect as using a static variable orsingleton, but integrates quite well into the existing Androidframework. Note that this will not work across processes (should yourapp be one of the rare ones that has multiple processes).
--这个效果就是使用静态变量是一样的,但是其更符合android的架构体系。
原文链接:http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables
分享到:
相关推荐
VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量VC中如何定义全局变量...
用MFC制作的工程由很多文件构成,它不能象一般C++程序那样随意在类外定义全局变量,在这里要想定义能被工程内多个文件共享的全局变量和函数必须用一些特殊方法才行。实际上有多种方法可以实现,这里只介绍两种方法。
mfc定义全局变量。
在AppDelegate类中轻松定义全局变量,几乎和C语言一样来定义。
MFC中定义全局变量和全局函数的方法,很不错的方法,网上可以搜到,所以积攒人品,0分
本篇文章主要介绍了浅谈在vue项目中如何定义全局变量和全局函数,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文主要跟大家介绍了关于Vue定义全局变量与常量的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍: 我想要定义一个变量, 在项目的任何地方都可以访问到, 不需要每一次使用的时候, 都引入...
在C语言中使用extern 关键字来定义全局变量的时候,我们需要在.h文件和.c文件中重复定义,这种重复,导致了出错几率的增加。 今天,在整理自己的代码的时候,考虑到我写的代码从一至终都是在一个cpp文件里面。于是,...
Spring MVC配置Freemarker全局变量,实现应用全路径
不能在头文件.h里面定义全局变量,如果头文件中可以定义全局变量,那么每个包含该头文件的文件里都会有该全局变量的定义.因为C语言的include是直接将文件嵌入到include这个地方的. ...
最近在学习VUE.js 中间涉及到JS全局变量,与其说是VUE的全局变量,不如说是模块化JS开发的全局变量。 1、全局变量专用模块 就是以一个特定模块来组织管理这些全局量,需要引用的地方导入该模块便好。 全局变量专用...
今天小编就为大家分享一篇在vue中多次调用同一个定义全局变量的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
第一:使用extern關鍵字聲明(不推薦,破壞了封裝性) 第二:新建一個類,存放全局的變量,函數 第一:使用extern關鍵字聲明(不推薦,破壞了封裝性) 第二
定义全局变量 原理: 设置一个专用的的全局变量模块文件,模块里面定义一些变量初始状态,用export default 暴露出去,在main.js里面使用Vue.prototype挂载到vue实例上面或者在其它地方需要使用时,引入该模块便可。...
定义count变量是全局变量,定义temp变量是局部变量, 函数func1中引用全局变量count,函数func1中调用函数func2