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的架构体系。
分享到:
相关推荐
大家好,今天给大家分享一下Android里的Context的一些用法. 这里大致可以分为两种:一是传递Context参数,二是调用全局的Context. 其实我们应用启动的时候会启动Application这个类,这个类是在AndroidManifest.xml...
Android Context使用例子.
简要介绍android context 的用法
android:name="工具类的路径" 调用: Toast.makeText(MainApplication.getContext(), "文本", Toast.LENGTH_SHORT).show(); 不管你想在项目的任何地方使用Context,只需要调用一下MainApplication.getContext()...
家好,今天给大家分享一下Android里的Context的一些用法,以前经常有人在群里问我比如我在一个工具类里的某个方法,或者View里需要调用Context.但是工具类还有View里没有这个上下文怎么办?为了解决大家的疑问,我...
Android全局变量和Context的实现方法
android_context详解,深入剖析
012_android 之消息提示toast 和Context视频教材,讲解的比较详细,有兴趣的可以学习下哦。
本文主要介绍Android中Context引起的内存泄露的问题,这里对Context的知识做了详细讲解,说明如何避免内存泄漏的问题,有兴趣的小伙伴可以参考下
主要介绍了Android 中Context的使用方法详解的相关资料,希望通过本文大家能够理解掌握context的使用方法,需要的朋友可以参考下
以下分别通过Context认知角度,继承关系,对象创建等方面android中Context做了深入的解释,一起学习下。 1、Context认知。 Context译为场景,一个应用程序可以认为是一个工作环境,在这个工作环境中可以存在许多场景...
android context理解
本文实例讲述了Android编程中context及全局变量的用法。分享给大家供大家参考,具体如下: 今天在研究context的时候,对application和activity context有了一定的了解,下面是从网上复制过来的资料 Application ...
android Context Menu With Icon
Android-Context-Menu.Android.zip,你可以很容易地添加令人敬畏的动画上下文菜单到你的应用程序。,安卓系统是谷歌在2008年设计和制造的。操作系统主要写在爪哇,C和C 的核心组件。它是在linux内核之上构建的,具有...
android demo,BaseAdapter传递context的应用,
Context-Menu.Android源码,是一个很不错的C++代码,有兴趣的伙伴们抽时间可以看一下把。
主要介绍了Android编程获取全局Context的方法,实例分析了基于Application类获取全局Context的实现步骤与相关技巧,需要的朋友可以参考下
主要介绍了Android中的Context抽象类,包括Context的实例化与获取App的Context等方法,需要的朋友可以参考下
Context用法详解