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仅供学习使用,转载请指明原作者. 文 ...
随机推荐
- Microsoft Visual Studio 2010导致系统C盘不断增大问题处理。
一直用Microsoft Visual Studio 2010做开发,发现最近C盘空间是越来越小,一开始以为是IE或者一些系统补丁造成的临时文件,但是使用360,windows优化大师之类的软件都清过 ...
- iScroll4插件的使用实例
iScroll是Matteo Spinelli开发的一个滚动插件,使用原生js编写,其不依赖与任何js框架.iScroll 4 完全重写了iScroll这个框架的原始代码.旨在解决移动webkit系浏 ...
- php curl 抓取
<?php set_time_limit(0); function curl_multi($urls) { if (!is_array($urls) or count($urls) == 0) ...
- Druid 配置_配置WebStatFilter
https://github.com/alibaba/druid/wiki/%E9%85%8D%E7%BD%AE_%E9%85%8D%E7%BD%AEWebStatFilter WebStatFilt ...
- JSP中Out和Request对象详解
内置表示不需要new便可直接使用. 一.基础知识 1.缓冲区:IO最原始是一个一个字节的读取,这就像吃米饭的时候一粒一粒的吃,很没有效率,这时候就有了碗,一碗一碗的吃,岂不痛快. 2.Get提交不能超 ...
- setTimeOut传參数
function blink(e_Id, second) {var soccer = document.getElementById(e_Id); soccer.style.visibility = ...
- java生成自己定义的表ID
需生成例如以下ID: 56d7ade1-87d1-4f54-8dc8-13611c8c2545 27181ad4-4214-4e12-af3a-911a0103a12f 24cafdfb-eac3-4 ...
- Web开发者需养成的8个好习惯
优秀的Web开发人员工作效率更高,因为他们拥有丰富的经验和良好的习惯.作者Gregor Dorfbauer分享了用于Web开发中的8个好习惯,这些良好的工作习惯不仅能提高效率,还能让您创建更加优秀的应 ...
- IOS UITableView删除功能
UITbableView作为列表展示信息,除了展示的功能,有时还会用到删除,比如购物车等.删除功能可以直接使用系统自带的删除功能,当横向轻扫cell时,右侧出现红色的删除按钮,点击删除当前cell. ...
- CSS3-border-radius 属性
向 div 元素添加圆角边框: div { border:2px solid; border-radius:25px; } IE9+.Firefox 4+.Chrome.Safari 5+ 以及 Op ...