activity之間通過全局變數傳遞數據 一、簡介 Application域中的onCreate方法是Android程式的入口,Android程式運行的時候就自動載入Application的對象,感覺Main方法應該是封裝在這個裡面的。 二、 ...
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>