activity之间通过全局变量传递数据
一、简介
Application域中的onCreate方法是Android程序的入口,Android程序运行的时候就自动加载Application的对象,感觉Main方法应该是封装在这个里面的。
activity中传递数据主要用intent方式和全局变量方式。
二、使用步骤
1、新建MyApplication类继承Application类
public class MyApplication extends Application
继承之后MyApplication类就是全局的了
2、在传输数据页面调用MyApplication类并生成数据
MyApplication app=(MyApplication) getApplication();
app.setName("fry");
app.setAge(22);
3、在接收数据页面调用MyApplication类并输出数据
MyApplication app=(MyApplication) getApplication();
tv_answer.setText("app:" + app );
4、在AndroidManifest.xml配置文件中添加MyApplication类的Android启动配置
android:name="activityGlobelVariable.MyApplication"
5、大功告成
三、具体代码实例
代码中进行了两次优化:
一是对MyApplication类实现了单例化模式,便于在没有继承Activity的类里面调用
调用:
MyApplication app = MyApplication.getInstance();
二是对于传输多组数据,使用了HashMap
private HashMap<String, Object> map=new HashMap<String, Object>();
结果图:
activityGlobelVariable.MyApplication
1 package activityGlobelVariable; 2 3 import java.util.HashMap; 4 5 import android.app.Application; 6 7 public class MyApplication extends Application{ 8 9 10 //也可以用hashMap来代替这些私有变量 11 private HashMap<String, Object> map=new HashMap<String, Object>(); 12 13 14 private String name; 15 private int age; 16 17 18 19 20 21 public HashMap<String, Object> getMap() { 22 return map; 23 } 24 25 public void setMap(HashMap<String, Object> map) { 26 this.map = map; 27 } 28 29 30 31 32 33 34 35 36 37 38 39 private static MyApplication instance=null; 40 41 public static MyApplication getInstance(){ 42 return instance; 43 } 44 45 public String getName() { 46 return name; 47 } 48 49 50 51 public void setName(String name) { 52 this.name = name; 53 } 54 55 56 57 58 public int getAge() { 59 return age; 60 } 61 62 63 64 public void setAge(int age) { 65 this.age = age; 66 } 67 68 69 70 71 public MyApplication() { 72 super(); 73 } 74 75 76 77 public MyApplication(String name, int age) { 78 super(); 79 this.name = name; 80 this.age = age; 81 } 82 83 84 85 @Override 86 public void onCreate() { 87 // TODO Auto-generated method stub 88 super.onCreate(); 89 this.instance=this; 90 } 91 92 93 94 95 96 97 98 99 100 101 102 @Override 103 public String toString() { 104 return "MyApplication [name=" + name + ", age=" + age + "]"; 105 } 106 107 108 109 110 }
activityGlobelVariable.MainActivity
1 package activityGlobelVariable; 2 3 4 5 6 import com.example.activityGlobelVariable.R; 7 8 import android.app.Activity; 9 import android.content.Intent; 10 import android.os.Bundle; 11 import android.view.View; 12 import android.view.View.OnClickListener; 13 import android.widget.Button; 14 15 16 17 public class MainActivity extends Activity{ 18 private Button btn_openActivty;//创建一个button对象 19 MyApplication app = MyApplication.getInstance(); 20 protected void onCreate(Bundle savedInstanceState) { 21 super.onCreate(savedInstanceState);//父类操作 22 setContentView(R.layout.activity_main);//引入名为activity_main的界面 23 btn_openActivty=(Button) findViewById(R.id.btn_openActivity);//找id为btn_openActivity的button 24 btn_openActivty.setOnClickListener(new OnClickListener() {//设置button点击监听 25 26 @Override 27 public void onClick(View v) {//onclick事件 28 // TODO Auto-generated method stub 29 Intent intent=new Intent(MainActivity.this,Activity01.class);//初始化intent 30 //Application域的访问 31 //MyApplication app=(MyApplication) getApplication(); 32 app.setName("fry"); 33 app.setAge(22); 34 35 //HashMap的应用 36 app.getMap().put("data1", "data1"); 37 app.getMap().put("data2", 2.2); 38 startActivity(intent);//打开activity 39 } 40 }); 41 } 42 @Override 43 protected void onDestroy() { 44 // TODO Auto-generated method stub 45 super.onDestroy(); 46 app.getMap().remove("data1"); 47 app.getMap().remove("data2"); 48 } 49 }
activityGlobelVariable.Activity01
package activityGlobelVariable; import com.example.activityGlobelVariable.R; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Activity01 extends Activity { private static final MyApplication MyApplication = null; private TextView tv_answer; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity01); tv_answer = (TextView) findViewById(R.id.tv_answer); /* * getApplication()是Activity类里面的方法,别的类没有 * 因为MyApplication继承了Application,是全局的 在配置文件里面要配MyApplication * MyApplication app=(MyApplication) getApplication(); * 上面这句话就是访问调用MyApplication */ // MyApplication app=(MyApplication) getApplication(); MyApplication app = MyApplication.getInstance(); // HashMap的应用 String s1 = (String) app.getMap().get("data1"); double d1 = (Double) app.getMap().get("data2"); tv_answer.setText("app:" + app + "\ns1:" + s1 + "\nd1:" + d1); } }
/activityGlobelVariable/AndroidManifest.xml
1 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 2 package="com.example.activityGlobelVariable" 3 android:versionCode="1" 4 android:versionName="1.0" > 5 6 <uses-sdk 7 android:minSdkVersion="8" 8 android:targetSdkVersion="19" /> 9 10 <application 11 android:name="activityGlobelVariable.MyApplication" 12 android:allowBackup="true" 13 android:icon="@drawable/ic_launcher" 14 android:label="@string/app_name" 15 android:theme="@style/AppTheme" > 16 <activity 17 android:name="activityGlobelVariable.MainActivity" 18 android:label="@string/app_name" > 19 <intent-filter> 20 <action android:name="android.intent.action.MAIN" /> 21 22 <category android:name="android.intent.category.LAUNCHER" /> 23 </intent-filter> 24 </activity> 25 <activity android:name="activityGlobelVariable.Activity01" android:exported="true"></activity> 26 </application> 27 28 </manifest>