`
jobs2010
  • 浏览: 26476 次
  • 性别: Icon_minigender_1
  • 来自: 南昌
最近访客 更多访客>>
社区版块
存档分类
最新评论

关于Android中是否可以使用全局变量的问题

阅读更多

The more general problem you are encountering is how to save stateacross several Activities and all parts of your application. A staticvariable (for instance, a singleton) is a common Java way of achievingthis. I have found however, that a more elegant way in Android is toassociate your state with the Application context.

--如想在整个应用中使用,在java中一般是使用静态变量,而在android中有个更优雅的方式是使用Application context。

As you know, each Activity is also a Context, which is informationabout its execution environment in the broadest sense. Your applicationalso has a context, and Android guarantees that it will exist as asingle instance across your application.
--每个Activity 都是Context,其包含了其运行时的一些状态,android保证了其是single instance的。

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 andmake it available for your entire application. You can access it fromany context using the Context.getApplicationContext() method (Activityalso provides a method getApplication() which has the exact sameeffect):
--方法是创建一个属于你自己的android.app.Application的子类,然后在manifest中申明一下这个类,这是android就为此建立一个全局可用的实例,你可以在其他任何地方使用Context.getApplicationContext()方法获取这个实例,进而获取其中的状态(变量)。

给个例子:

  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 orsingleton, but integrates quite well into the existing Androidframework. Note that this will not work across processes (should yourapp be one of the rare ones that has multiple processes).
--这个效果就是使用静态变量是一样的,但是其更符合android的架构体系。

原文链接:http://stackoverflow.com/questions/708012/android-how-to-declare-global-variables

分享到:
评论

相关推荐

    全局变量的使用

    如果想在整个应用中使用全局变量,在java中一般是使用静态变量,public类型;而在android中如果使用这样的全局变量就不符合Android的框架架构,但是可以使用一种更优雅的方式就是使用Application context。 首先...

    android 全局变量使用

    android 全局变量的定义使用

    Android中用Application类实现全局变量

    Android中用Application类实现全局变量

    Android全局变量和Context

    Android全局变量和Context的实现方法

    Android中的全局变量与局部变量使用小结

    主要介绍了Android中的全局变量与局部变量使用小结,全局变量顾名思义就是在整个的类中或者可在多个函数中调用的变量,也称为外部变量,局部变量则是特定过程或函数中可以访问的变量,需要的朋友可以参考下

    Android通过全局变量传递数据

    在Activity之间数据传递中还有一种比较实用的方式 就是全局对象 实用J2EE的读者来说都知道Java Web的四个作用域 这四个作用域从小到大分别是Page Request Session和Application 其中Application域在应用程序的任何...

    Android编程中全局变量问题分析

    本文实例讲述了Android编程中全局变量。分享给大家供大家参考,具体如下: 现在每天都在忙,而且一忙起来,就把写笔记的事情放在了后面,最近在写程序的时候,突然要使用全局变量,就按照以前的方式,写了一个类,...

    Android编程之Application设置全局变量及传值用法实例分析

    本文实例讲述了Android编程之Application设置全局变量及传值用法。分享给大家供大家参考,具体如下: /** * 重写Application,主要重写里面的onCreate方法,就是创建的时候, * 我们让它初始化一些值,前段时间在...

    Eclipse编写的Android全局变量应用实例

    Eclipse V4.2.0编写的Android全局变量应用实例。

    详解Android中Application设置全局变量以及传值

    Application设置全局变量以及传值 /** * 重写Application,主要重写里面的onCreate方法,就是创建的时候, * 我们让它初始化一些值,前段时间在javaeye里面看到过一个例子,与此相似, * 我做了些改进。听说外国...

    Android-Application被回收引发空指针异常分析(消灭全局变量

    Android-Application被回收引发空指针异常分析(消灭全局变量)-例子 http://blog.csdn.net/zivensonice/article/details/51451486

    Android编程中context及全局变量实例详解

    本文实例讲述了Android编程中context及全局变量的用法。分享给大家供大家参考,具体如下: 今天在研究context的时候,对application和activity context有了一定的了解,下面是从网上复制过来的资料 Application ...

    android系统中几种系统级别的全局变量

    android系统中几种系统级别的全局变量 在android 开发中时,尤其是在开发调试系统应用的时候,有时候需要设置一个系统级别的flag标志位,来提供给几个应用使用判断。例如开机完成后,或者走完开机导航后,都需要设置...

    android jni使用static变量

    这是我自己写的android jni调用java static属性的例子

    浅谈Android中关于静态变量(static)的使用问题

    在这里记录下Android中使用static的一些问题。 静态变量的生命周期遵守Java的设计。静态变量在类被load的时候分配内存,并存在于方法区。当类被卸载时,静态变量被销毁。在PC机的客户端程序中,一个类被加载和卸载,...

Global site tag (gtag.js) - Google Analytics