如何android多Activity间共享数据 (extends Application)

1.定义全局变量

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()方法获取这个实例,进而获取其中的状态(变量)。

给个例子:

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 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的架构体系。

注意APP必须在manifest注册(




package com.xesam;

import android.app.Application;
import android.content.res.Configuration;
import android.util.Log;

public class ApplicationActivity extends Application {
    static String name = "小西山子";
    @Override
    public void onCreate() {
        Log.v("ApplicationActivity","onCreate");
        super.onCreate();
    }
    @Override
    public void onTerminate() {
        Log.v("ApplicationActivity","onTerminate");
        super.onTerminate();
    }
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.v("ApplicationActivity","onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }
    @Override
    public void onLowMemory() {
        Log.v("ApplicationActivity","onLowMemory");
        super.onLowMemory();
    }
}
 
主要Activity:
 
package com.xesam;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        Log.v("MainActivity","onCreate");
        super.onCreate(savedInstanceState);
       
        ApplicationActivity app = (ApplicationActivity)this.getApplication();
       
        LinearLayout main = new LinearLayout(this);
        main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        main.setOrientation(LinearLayout.VERTICAL);
        TextView tv = new TextView(this);
        tv.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
        tv.setText(app.name);
        tv.setTextColor(Color.BLUE);
        main.addView(tv);
        setContentView(main);
    }
    @Override
    public void onDestroy(){
        Log.v("MainActivity","onDestroy");
        super.onDestroy();
        //System.exit(0);
    }
}
 
AndroidManifest.xml:
 

    package="com.xesam"
    android:versionCode="1"
    android:versionName="1.0" >

   

            android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:name="ApplicationActivity">
                    android:name=".MainActivity"
            android:label="@string/app_name" >
           
               

               
           
       
   


 
这里我们自定义了一个Application,所以必须指定AndroidManifest.xml中application的android:name属性(默认是android.app.Application)。
输出结果:
"ApplicationActivity","onCreate"
"MainActivity","onCreate"
"MainActivity","小西山子"
"ApplicationActivity","onConfigurationChanged"
退出之后打印结果:
"MainActivity","onDestroy"
从模拟器再次开启应用(没有再次编译)之后,打印结果
"MainActivity","onCreate"
"MainActivity","小西山子"
所以后退的时候并没有关闭application,要强制关闭application,可以在MainActivity的onDestroy中调用System.exit(0)
    @Override
    public void onDestroy(){
        Log.v("MainActivity","onDestroy");
        super.onDestroy();
        System.exit(0);
    }
这样就完全退出程序了。
 
回到application的问题上来,显然,application的onCreate在activity的onCreate之前执行了,所以整个应用的最初入口是这个方法。
onCreate作用说明:
 
    /**
     * Called when the application is starting, before any other application
     * objects have been created.  Implementations should be as quick as
     * possible (for example using lazy initialization of state) since the time
     * spent in this function directly impacts the performance of starting the
     * first activity, service, or receiver in a process.
     * If you override this method, be sure to call super.onCreate().
     */
 
可以认为Activity,service以及其他的组件共同构成了一个application的环境,因此application中可以共享或者初始化一些全局变量,比如地图(百度地图demo就是这么做的)。
 
 
注:eclipse查看源码的方法:
下载android版本对应的源码【2.2版本地址:http://rgruet.free.fr/public/android-2.2-froyo-src.zip】
解压缩到 /usr/local/android-sdk-linux/platforms/android-8下,然后修改同目录下source.properties文件中的Pkg.SourceUrl为刚才解压的源码目录就行了
一个示例(我下载的源码放在/usr/local/android-sdk-linux/platforms/android-8/source目录中,所以Pkg.SourceUrl=sources):
 
### Android Tool: Source of this archive.
#Mon Apr 23 01:14:48 CST 2012
Layoutlib.Api=4
Layoutlib.Revision=0
Pkg.Desc=Android SDK Platform 2.2_r3
Archive.Arch=ANY
Platform.Version=2.2
Pkg.DescUrl=http\://developer.android.com/sdk/
Platform.MinToolsRev=8
Archive.Os=ANY
Pkg.SourceUrl=sources
Pkg.Revision=3
AndroidVersion.ApiLevel=8


你可能感兴趣的:(android)