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. Linux知识(4)----文件系统结构

    Ubantu 14.04的文件系统结构如下图所示: 参考资料: 1.http://www.cnblogs.com/wen858636827/archive/2012/12/26/2834373.htm ...

  2. Centos 6/ 7下通过yum安装php7环境

    本文转自:云溪社区 2015年12月初PHP7正式版发布,迎来自2004年以来最大的版本更新.PHP7最显著的变化就是性能的极大提升,已接近Facebook开发的PHP执行引擎HHVM.在WordPr ...

  3. IOS上传图片方法类

    IOS上传图片方法类   iPhone开发中遇到上传图片问题,找到多资料,最终封装了一个类,请大家指点,代码如下 // // RequestPostUploadHelper.h // demodes ...

  4. Mr.Xu的找实习之路

    长长的路慢慢走 深深的话浅浅说 --广工Mr.Xu的找前端实习之路 这 不(display:none) 是 广 告 本人广工大三学生一枚,也是学校TopView团队的成员之中的一个.之前我们团队有位屌 ...

  5. Div+CSS展示物流跟踪轨迹信息

    <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <m ...

  6. ORACLE的VARCHAR2是字节还是字符

    往Oracle一个表的VACHAR2(20)字段中插入七个汉字,提示错误:插入的值太大. 改成插入六个汉字,又可以. 于是百度,原来这与ORACLE的字符集设置有关.(以前的项目都是设置成的ZHS16 ...

  7. USACO ariprog 暴力枚举+剪枝

    /* ID:kevin_s1 PROG:ariprog LANG:C++ */ #include <iostream> #include <cstdio> #include & ...

  8. [Todo] Redis里面队列的两种模式,以及抢红包在Redis中的实现

    两种队列模式: 一种是利用list的lpush/rpop等 另一种是redis自带的发布者/订阅者模式 http://www.cnblogs.com/alazalazalaz/p/5512258.ht ...

  9. SQL VM上磁盘延迟高, 但Host和Storage Array上的延迟却很低的问题

    按照下面的步骤, 问题解决. =========================== Per Microsoft DDK, Microsoft storport.sys maintains a dev ...

  10. Node.js:Buffer(缓冲区)介绍及常用方法

    JavaScript 语言自身只有字符串数据类型,没有二进制数据类型. 但在处理像TCP流或文件流时,必须使用到二进制数据.因此在 Node.js中,定义了一个 Buffer 类,该类用来创建一个专门 ...