Android--全局变量 很好很强大
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):
- 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 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中添加应用:
- <application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".ClickableListItemActivity"
- android:label="@string/app_name">
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- </application>
说明:
需添加的内容:android:name=".your_App_Name"
位置:当前activity所在的位置,(我刚开始以为需要新建一个<application></application>)
Android--全局变量 很好很强大的更多相关文章
- Android全局变量使用
1.通过一个Data.java实例类,能够实现全局数据保存,这里就不多说了,学Java的都知道. 2.Android特有的Application,是应用的入口,执行贯穿整个app执行过程,能够在这个类 ...
- 【Android 应用开发】 Application 使用分析
博客地址 : http://blog.csdn.net/shulianghan/article/details/40737419 代码下载 : Android 应用 Application 经典用法; ...
- 两种不同的Context
本文转载于:http://blog.csdn.net/xiaodongvtion/article/details/8443772 这是两种不同的context,也是最常见的两种.第一种中context ...
- listview分页载入问题
方案一: 底部有查看很多其它能够使用HeaderViewListAdapter 假设须要加入数据, 就向Adapter绑定的数据里面加入. 然后调用Adapter.notifyDataSetChang ...
- Application 使用分析
一. Application 分析 1. Application 简介 (1) Application 概念 Application 概念 : Application 属于组件范畴; -- 本质 : ...
- 分享45个android实例源码,很好很强大
分享45个android实例源码,很好很强大 http://www.apkbus.com/android-20978-1-1.html 分享45个android实例源码,很好很强大http://www ...
- Android 使用全局变量传递数据
使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一 ...
- 在android.app.Application中定义全局变量
在Android应用中使用全局变量,除了public的静态变量,还有更优雅的方式是使用android.app.Application. 启动Application时,系统会创建一个PID,即进程ID, ...
- 关于在Android设置全局变量随时获取context
最实在的办法就是继承Application,在里面设置全局变量,因为Application是android的应用入口,并且运行周期贯穿整个程序运行. import android.app.Applic ...
- 转:Android设置全局变量
声明:本文转自feiyangxiaomi的博客:http://blog.csdn.net/feiyangxiaomi/article/details/9966215仅供学习使用,转载请指明原作者. 文 ...
随机推荐
- 更新teaching中fdSubjectID为null的老数据
UPDATE wkwke.tbTeachingV3 teaching SET teaching.fdSubjectID = ( SELECT fdValue FR ...
- printf回到上一行开头以及回到本行开头的方法
回到上一行开头 #include <stdio.h> #include <unistd.h> int main(void) { ; ){ printf("%d\n&q ...
- ajax成功返回数据中存在多余字符的处理
ajax里有需要判断反馈的字符串是否为“ok”,在浏览器里调试,看到返回的内容明明是“ok”,但是if(“ok”==data)判断为false,用alert打印内容也是ok,但是打印长度的时候却是3. ...
- [Mac OS] Homebrew简介及安装wine
Homebrew官网 http://brew.sh/index_zh-cn.html Homebrew是神马 linux系统有个让人蛋疼的通病,软件包依赖,好在当前主流的两大发行版本都自带了解决方案, ...
- iOS开源项目:AudioPlayer
AudioPlayer是一个基于AVAudioStreamer的在线音乐播放软件. https://github.com/marshluca/AudioPlayer 首先将歌曲信息存储在NSArray ...
- Proxmark3命令帮助
Proxmark3命令帮助 目录 [隐藏] 1 使用技巧 2 help 主帮助命令(基于r830以及以下版本) 3 hw 硬件检测相关命令 4 data 图形窗口/缓冲区数据操作等命令 5 ...
- c++ 读写Excel及数据导入SQLServer
c++ 读写Excel及数据导入SQLServer C++操作Excel ,网上的资料还是比较多的,写这篇文章也是分享给初学者一些经验. 本人 觉得CSpreadShe ...
- 当Windows Server 2012的主DC出了问题, 如何迁移其上的FSMO角色?
步骤如下: 1. 遵循https://support.microsoft.com/kb/255504中的transfer FSMO的步骤. 2. 这样做之后还没完. 因为Windows Server ...
- Windows-设置系统服务不开机启动
设置为手动,则开机不会自动启动了
- Nginx安装部署(反向代理与负载均衡)
一.下载安装Nginx(本文环境为windows xp 32bit环境) 下载地址:http://files.cnblogs.com/likehua/nginx-1.0.11.zip 解压nginx- ...