在Java中如果要使用全局变量,一般定义public static类型的变量。但是这种方法不符合Android的框架架构,Android中要使用Application context。

Application是一个基类,这个基类的作用是获取整个App的状态,我们需要自己定义一个类来继承这个基类。代码如下:

package com.tianjf; import android.app.Application; public class MyApplication extends Application { private boolean mHasPassword; public boolean ismHasPassword() { return mHasPassword; } public void setmHasPassword(boolean mHasPassword) { this.mHasPassword = mHasPassword; } @Override public void onCreate() { mHasPassword = true; super.onCreate(); } } 

我们定义了一个MyApplication继承自Application,并定义了一个全局变量mHasPassword,然后复写基类的onCreate方法,onCreate负责对所有全局变量赋初期值。

我们还需要把自定义的Application类添加到AndroidManifest.xml里面,代码如下:

<application android:name="MyApplication" 。。。。。。。。。。。。。。。。。。。。。。。。。。。 。。。。。。。。。。。。。。。。。。。。。。。。。。。 </application> 

这样做的目的:App的进程被创建的时候,这个类就会被实例化,onCreate方法就会被执行,给所有全局变量赋初期值。

这样,所有的Activity就共同拥有这个类里面的变量了。

下面用两个Activity来测试一下,当一个Activity改变了全局变量的值之后,看看另一个Activity能不能取到改变后的值。

ApplicationDemoActivity.java

package com.tianjf; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; public class ApplicationDemoActivity extends Activity implements OnClickListener { private static final String TAG = "ApplicationDemoActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); findViewById(R.id.button).setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: MyApplication myApplication = (MyApplication) getApplication(); Log.i(TAG, String.valueOf(myApplication.ismHasPassword())); myApplication.setmHasPassword(false); Intent intent = new Intent(this, AnotherActivity.class); startActivity(intent); break; default: break; } } } 

AnotherActivity.java

package com.tianjf;    import android.app.Activity;  import android.os.Bundle;  import android.util.Log;    public class AnotherActivity extends Activity {        private static final String TAG = "AnotherActivity";            @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.another);          MyApplication myApplication = (MyApplication) getApplication();          Log.i(TAG, String.valueOf(myApplication.ismHasPassword()));      }  }  

main.xml

<?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="fill_parent"      android:layout_height="fill_parent"      android:orientation="vertical" >        <TextView          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="@string/hello" />        <Button          android:id="@+id/button"          android:layout_width="fill_parent"          android:layout_height="wrap_content"          android:text="Start another activity" />    </LinearLayout>  

another.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 


更多相关文章

  1. Android(安卓)中自定义控件和属性(attr.xml,declare-styleable,T
  2. android 自定义view 实现定制二维码扫描框
  3. android Audio 详解( 一 )
  4. 自定义控件
  5. android binder机制之--(创建binder服务)
  6. Android(安卓)TabHost学习笔记
  7. Android高手进阶教程(三)之----Android(安卓)中自定义View的应用
  8. Android(安卓)中自定义控件和属性(attr.xml,declare-styleable,T
  9. Android(安卓)自定义阴影效果详解及实例

随机推荐

  1. 大禹是从死去的男人肚子里蹦出来
  2. 黄帝建国版图与他的足迹
  3. 使用flex、grid跟定位布局仿一个简易的淘
  4. 探秘黄帝与中华文明的起源
  5. CSS伪类选择器、box-sizing属性、常用单
  6. 为什么说网站更新,要有规律?良好的更新频率
  7. 一篇文章告诉你SEO究竟该如何入门?
  8. HTML常用元素样式、定位学习与应用
  9. PHP检查空值的方法总结
  10. php操作redis命令及代码实例大全