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();
- }
- }
我们还需要把自定义的Application类添加到AndroidManifest.xml里面,代码如下:
- <application
- android:name="MyApplication"
- 。。。。。。。。。。。。。。。。。。。。。。。。。。。
- 。。。。。。。。。。。。。。。。。。。。。。。。。。。
- </application>
这样,所有的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;
- }
- }
- }
- 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()));
- }
- }
- <?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>
- <?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>