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. PHP自动加载下——PSR4

    1.先来介绍一下PSR规范 PHP-FIG,它的网站是:www.php-fig.org.就是这个联盟组织发明和创造了PSR规范,其中自动加载涉及其中两个规范,一个是PSR0,一个是PSR4, PSR0 ...

  2. vue2.0中引入UEditor的一些坑。。。。

    开发后台系统的时候,富文本编辑器肯定是必不可少的,然后呢~在天朝当然要属百度编辑器(UEditor)最成熟了,功能全面,文档齐全(相对),ui优美(...,对于程序员来说)等等许多方面(MMP,还不是 ...

  3. Windows 配置 Apache Python CGI

    提示:安装Apache可参考 https://jingyan.baidu.com/article/0eb457e53c019f03f1a905c7.html 1.  打开URL: https://ww ...

  4. Python学习(四)数据结构 —— set frozenset

    集合类型 set  frozenset 赋值及去重 set 是一个无序不重复元素集,还有个frozenset 类型(顾明思议,就是不可改变元素的集合): 基本功能包括关系测试和消除重复元素:set支持 ...

  5. getaddrinfo详解

    #include <sys/socket.h> #include <netdb.h> int getaddrinfo(const char *restrict nodename ...

  6. 数值格式化 NumberFormat DecimalFormat RoundingMode

    NumberFormat [简介] java.text.NumberFormat extends java.text.Format extends java.lang.Object 实现的接口:Ser ...

  7. 关于docker的15个小tip

    1. 获取最近运行容器的id 这是我们经常会用到的一个操作,按照官方示例,你可以这样做(环境ubuntu): $ ID=$(docker run ubuntu echo hello world) he ...

  8. 【云计算】jenkins,docker,mesos,marathon,k8s相关资料

    参考资料: 基于Apache Mesos 构建高可靠,高可用的Jenkins CI:http://blog.csdn.net/ebay/article/details/43529401 Docker ...

  9. 论#include

    1.#include " "与#include <> #include " "表示预编译命令源程序在当前项目下寻找头文件,如果找不到,再到标准头文件 ...

  10. 如何解决Ubuntu与Windows双系统时间不同步

    导读 不知道有没朋友跟我一样是 Ubuntu 和 Windows 双系统?今天有朋友问到我,当他从 Ubuntu 系统重新启动到 Windows 时,会发现 Windows 中的时间变了,他问我有没办 ...