As you know, each Activity is also a Context, which is information about its execution environment in the broadest sense. Your application also has a context, and Android guarantees that it will exist as a single instance across your application.

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 and make it available for your entire application. You can access it from any context using the Context.getApplicationContext() method (Activity also provides a method getApplication() which has the exact same effect):

  1. class MyApp extends Application {
  2. private String myState;
  3. public String getState(){
  4. return myState;
  5. }
  6. public void setState(String s){
  7. myState = s;
  8. }
  9. }
  10. class Blah extends Activity {
  11. @Override
  12. public void onCreate(Bundle b){
  13. ...
  14. MyApp appState = ((MyApp)getApplicationContext());
  15. String state = appState.getState();
  16. ...
  17. }
  18. }

This has essentially the same effect as using a static variable or singleton,

but integrates quite well into the existing Android framework.

Note that this will not work across processes (should your app be one of the rare ones that has multiple processes).

然后再manifest中添加应用:

  1. <application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">
  2. <activity android:name=".ClickableListItemActivity"
  3. android:label="@string/app_name">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>
  9. </application>

说明:

  1. 需添加的内容:android:name=".your_App_Name"

  2. 位置:当前activity所在的位置,(我刚开始以为需要新建一个<application></application>)

Android--全局变量 很好很强大的更多相关文章

  1. Android全局变量使用

    1.通过一个Data.java实例类,能够实现全局数据保存,这里就不多说了,学Java的都知道. 2.Android特有的Application,是应用的入口,执行贯穿整个app执行过程,能够在这个类 ...

  2. 【Android 应用开发】 Application 使用分析

    博客地址 : http://blog.csdn.net/shulianghan/article/details/40737419 代码下载 : Android 应用 Application 经典用法; ...

  3. 两种不同的Context

    本文转载于:http://blog.csdn.net/xiaodongvtion/article/details/8443772 这是两种不同的context,也是最常见的两种.第一种中context ...

  4. listview分页载入问题

    方案一: 底部有查看很多其它能够使用HeaderViewListAdapter 假设须要加入数据, 就向Adapter绑定的数据里面加入. 然后调用Adapter.notifyDataSetChang ...

  5. Application 使用分析

    一. Application 分析 1. Application 简介 (1) Application 概念 Application 概念 : Application 属于组件范畴; -- 本质 : ...

  6. 分享45个android实例源码,很好很强大

    分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html 分享45个android实例源码,很好很强大http://www ...

  7. Android 使用全局变量传递数据

    使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一 ...

  8. 在android.app.Application中定义全局变量

    在Android应用中使用全局变量,除了public的静态变量,还有更优雅的方式是使用android.app.Application. 启动Application时,系统会创建一个PID,即进程ID, ...

  9. 关于在Android设置全局变量随时获取context

    最实在的办法就是继承Application,在里面设置全局变量,因为Application是android的应用入口,并且运行周期贯穿整个程序运行. import android.app.Applic ...

  10. 转:Android设置全局变量

    声明:本文转自feiyangxiaomi的博客:http://blog.csdn.net/feiyangxiaomi/article/details/9966215仅供学习使用,转载请指明原作者. 文 ...

随机推荐

  1. What is the Linux High Availabi

    What is the Linux High Availabi    简介:     高可用性群集的出现是为了使群集的整体服务尽可能可用,以便考虑计算硬件和软件的易错性.如果高可用性群集中的主节点发生 ...

  2. IOS开发自定义CheckBox控件

    IOS本身没有系统的CheckBox组件,但是实际开发中会经常用到,所以专门写了一个CheckBox控件,直接上代码 效果图: UICheckBoxButton.h文件如下: #import #imp ...

  3. [FireDAC][Phys][MySQL] MySQL server has gone away

    [FireDAC][Phys][MySQL]  MySQL server has gone away 原因: MYSQL等其它数据库,其本质上还是SOCKET服务端. 作为网络通信服务端都有一个机制— ...

  4. Ruby:对象模型(又称八卦模型)笔记

    备注 如果说哪门语言对我影响最大,那就是Ruby了,在.NET阵营多年,试图去用C#的思维去解释很多东西,当然解释Java是足够了,可惜我也用了好几年去解释Javascript,结果是可想而知的:解释 ...

  5. mysql中函数greatest 与MAX区别

    greatest (a,b,c,d,d)max(a) 这样就能看明白了,greatest 求的是某几列的最大值,横向求最大(一行记录)max(a) 一看就明白了,是给纵向求最大(多行记录).

  6. 实现SQL Server中的切割字符串SplitString函数

    有时我们要用到批量操作时都会对字符串进行拆分,可是SQL Server中却没有自带Split函数,所以要自己来实现了.没什么好说的,需要的朋友直接拿去用吧 SET ANSI_NULLS ON GO S ...

  7. iScroll示例,下拉刷新,上拉刷新

    iScroll示例,下拉刷新,上拉刷新 <!DOCTYPE html> <html> <head> <meta http-equiv="Conten ...

  8. 转:mac下安装Sublime Text

    转:http://blog.sina.com.cn/s/blog_559d66460101cab0.html 正版的买个license其实并不贵,定价为70美元.如果不买license,也可acces ...

  9. android AIDL 入门讲解非常好的文章(网页代码着色给力)

    转自: http://tech.cnnetsec.com/585.html

  10. ckeditor 实现图片上传以及预览(亲测有效)

    引用ckeditor <script type="text/javascript" src="static/ckeditor/ckeditor.js"&g ...