使用Bundle在Activity之間傳遞數(shù)據(jù)
- Bundle類是一個(gè)key-value對(duì),是一個(gè)final類
- 兩個(gè)Activity之間通信可以用Bundle類實(shí)現(xiàn)步驟:
- 新建一個(gè)Bundle類
Bundle bundle=new Bundle(); - bundle類中加入數(shù)據(jù)(key-value的形式)
bundle.putString("data", "張三"); - 新建一個(gè)Intent對(duì)象,并將該Bundle加入該Intent對(duì)象
Intent intent=new Intent();
intent.setClass(TestBundle.this, Target.class);
intent.putExtras(bundle); - 啟動(dòng)Intent
startActivity(intent); - 第二個(gè)Activity接收傳遞過(guò)來(lái)的數(shù)據(jù)
Intent intent=getIntent();
Bundle bundle =intent.getExtras();
String data = bundle.getString("data");//讀出數(shù)據(jù)
- 傳遞的數(shù)據(jù)類型
簡(jiǎn)單或者基本數(shù)據(jù)類型boolean、byte、char、String、Long、Short、Int等類型數(shù)據(jù)
使用Intent在Activity之間傳遞數(shù)據(jù)
- 步驟:
- 傳遞數(shù)據(jù)
Intent intent=new Intent();
//鍵值對(duì)
intent.putExtra("extra", "archie2010");
//從此ctivity傳到另一Activity
intent.setClass(OneActivity.this, OtherActivity.class);
//啟動(dòng)另一個(gè)Activity
OneActivity.this.startActivity(intent); - 接收數(shù)據(jù)
//獲得從上一個(gè)Activity傳來(lái)的intent對(duì)象
Intent intent=getIntent();
String stringValue=intent.getStringExtra("extra");
- 傳遞基本數(shù)據(jù)類型數(shù)據(jù)
Intent putExtra(String name, int[] value)
Intent putExtra(String name, float value)
Intent putExtra(String name, byte[] value)
Intent putExtra(String name, long[] value)
Intent putExtra(String name, float[] value)
Intent putExtra(String name, long value)
Intent putExtra(String name, String[] value)
Intent putExtra(String name, boolean value)
Intent putExtra(String name, boolean[] value)
Intent putExtra(String name, short value)
Intent putExtra(String name, double value)
Intent putExtra(String name, short[] value)
Intent putExtra(String name, String value)
Intent putExtra(String name, byte value)
Intent putExtra(String name, char[] value)
Intent putExtra(String name, CharSequence[] value)
序列化
- 什么是序列化 、反序列化
對(duì)象的序列化 : 把Java對(duì)象轉(zhuǎn)換為字節(jié)序列并存儲(chǔ)至一個(gè)儲(chǔ)存媒介的過(guò)程。
對(duì)象的反序列化:把字節(jié)序列恢復(fù)為Java對(duì)象的過(guò)程。
Activity之間還可以傳遞類的對(duì)象,傳遞之前必須要進(jìn)行序列化。
(在Android里面要想在Activity之間傳遞類的對(duì)象,必須將類進(jìn)行序列化) - 序列化的目的
- 永久的保存對(duì)象數(shù)據(jù)(將對(duì)象數(shù)據(jù)保存在文件當(dāng)中,或者是磁盤中)
- 通過(guò)序列化操作將對(duì)象數(shù)據(jù)在網(wǎng)絡(luò)上進(jìn)行傳輸(由于網(wǎng)絡(luò)傳輸是以字節(jié)流的方式對(duì)數(shù)據(jù)進(jìn)行傳輸?shù)?因此序列化的目的是將對(duì)象數(shù)據(jù)轉(zhuǎn)換成字節(jié)流的形式)
- 將對(duì)象數(shù)據(jù)在進(jìn)程之間進(jìn)行傳遞(Activity之間傳遞對(duì)象數(shù)據(jù)時(shí),需要在當(dāng)前的Activity中對(duì)對(duì)象數(shù)據(jù)進(jìn)行序列化操作.在另一個(gè)Activity中需要進(jìn)行反序列化操作講數(shù)據(jù)取出)
- Java平臺(tái)允許我們?cè)趦?nèi)存中創(chuàng)建可復(fù)用的Java對(duì)象,但一般情況下,只有當(dāng)JVM處于運(yùn)行時(shí),這些對(duì)象才可能存在,即,這些對(duì)象的生命周期不會(huì)比JVM的生命周期更長(zhǎng)(即每個(gè)對(duì)象都在JVM中)但在現(xiàn)實(shí)應(yīng)用中,就可能要停止JVM運(yùn)行,但有要保存某些指定的對(duì)象,并在將來(lái)重新讀取被保存的對(duì)象。這是Java對(duì)象序列化就能夠?qū)崿F(xiàn)該功能。(可選擇入數(shù)據(jù)庫(kù)、或文件的形式保存)
- 序列化對(duì)象的時(shí)候只是針對(duì)變量進(jìn)行序列化,不針對(duì)方法進(jìn)行序列化.
- 在Intent之間,基本的數(shù)據(jù)類型直接進(jìn)行相關(guān)傳遞即可,但是一旦數(shù)據(jù)類型比較復(fù)雜的時(shí)候,就需要進(jìn)行序列化操作了.
序列化的方式
1.自定義類實(shí)現(xiàn)Serializable接口
public class Student implements Serializable{
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
然后傳遞自定義類的對(duì)象
Bundle.putSerializable(Key,Object); //實(shí)現(xiàn)Serializable接口的對(duì)象
- 使用Bundle在Activity之間傳遞對(duì)象數(shù)據(jù)
傳遞數(shù)據(jù)如下:
Intent intent=new Intent(MainActivity.this,MyActivity.class
Bundle bundle=new Bundle();
Student student=new Student();
student.setName("張三");
bundle.putSerializable("student",student); startActivity(intent);
接收數(shù)據(jù)如下:
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
Student student=(Student) bundle.getSerializable("student");
2.自定義類實(shí)現(xiàn)Parcelable接口
- 步驟:
a、復(fù)寫describeContents方法和writeToParcel方法
b、實(shí)例化靜態(tài)內(nèi)部對(duì)象CREATOR,實(shí)現(xiàn)接口Parcelable.Creator
將變量寫出
@Override
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeInt(age);
}
將變量進(jìn)行讀取
public static final Creator<Student1> CREATOR =new Creator<Student1>(){
};
序列化之后就可以傳遞對(duì)象了,傳遞方式如下:
使用Bundle在Activity之間傳遞對(duì)象數(shù)據(jù)
傳遞數(shù)據(jù)如下:
Intent intent=new Intent(MainActivity.this,MyActivity.class
Bundle bundle=new Bundle();
Student1 student=new Student1();
student.setName("張三");student.setage(20)
bundle.putParcelable("student",student);
intent.putExtras(bundle);
startActivity(intent);
接收數(shù)據(jù)如下:
Intent intent=getIntent();
Bundle bundle=intent.getExtras();
Student1 student=(Student) bundle.getParcelable("student");
Activity跳轉(zhuǎn)回傳
- 在一些情況下,我們通過(guò) A activity跳轉(zhuǎn)到 B activity上,這時(shí)希望 A activtiy能從 B activity上得到一些返回值,這個(gè)時(shí)候我們就不能使用startActivity方法了,而是使用 startActivityForResult方法來(lái)完成我們的操作。
- 舉個(gè)例子:頁(yè)面1跳轉(zhuǎn)到頁(yè)面2,頁(yè)面2再返回頁(yè)面1同時(shí)返回?cái)?shù)據(jù)
步驟:
- 頁(yè)面1添加如下代碼:
Intent intent = new Intent();
intent.setClass(頁(yè)面1.this, 頁(yè)面2.class);
Bundle bundle = new Bundle();
bundle.putInt(“a”,2);
bundle.putInt(“b”,3);
intent.putExtras(bundle);
startActivityForResult(intent, 0);// 跳轉(zhuǎn)并要求返回值,0代表請(qǐng)求值(可以隨便寫)
- 頁(yè)面2接收數(shù)據(jù):
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
int a=bundle.getInt("a");//獲取頁(yè)面1傳遞過(guò)來(lái)的數(shù)
int b=bundle.getInt(“b”);
int c=a+b;
- 頁(yè)面2設(shè)置返回頁(yè)面1數(shù)據(jù):
Intent intent = new Intent;
Bundle bundle = new Bundle();
bundle.putInt(“sum”,c);
intent.putExtras(bundle);
setResult(RESULT_OK, intent);
4.頁(yè)面1接收返回?cái)?shù)據(jù):(需要重寫onActivityResult)
onActivityResult(int requestCode, int resultCode, Intent data)
第一個(gè)參數(shù):這個(gè)整數(shù)requestCode用于與startActivityForResult中的requestCode中值進(jìn)行比較判斷,是以便確認(rèn)返回的數(shù)據(jù)是從哪個(gè)Activity返回的。
第二個(gè)參數(shù):這整數(shù)resultCode是由子Activity通過(guò)其setResult()方法返回。適用于多個(gè)activity都返回?cái)?shù)據(jù)時(shí),來(lái)標(biāo)識(shí)到底是哪一個(gè)activity返回的值。
第三個(gè)參數(shù):一個(gè)Intent對(duì)象,帶有返回的數(shù)據(jù)??梢酝ㄟ^(guò)data.getXxxExtra( );方法來(lái)獲取指定數(shù)據(jù)類型的數(shù)據(jù)
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0&&resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
int sum=bundle.getInt("sum");
Toast.makeText(MainActivity.this, sum+"", Toast.LENGTH_LONG).show();
}
}
Activity跳轉(zhuǎn)回傳注意事項(xiàng)
protected void onActivityResult(int requestCode, int resultCode, Intent data)
方法里面的第一個(gè)參數(shù),必須要跟startActivityForResult(intent, 0)一致;
第二個(gè)參數(shù),必須與setResult(RESULT_OK, intent2)一致,判斷是同一個(gè)數(shù)字即可
原因:
如果出現(xiàn)兩個(gè)以上回傳,請(qǐng)求碼設(shè)置不同的,回傳回來(lái)的數(shù)據(jù)處理就需要根據(jù)不同的requestCode進(jìn)行分別處理。
小結(jié)
- Activity之間可以用Bundle傳遞數(shù)據(jù),但是Bundle必須放到Intent里面,最后通過(guò)Intent傳遞數(shù)據(jù)
- Activity之間進(jìn)行數(shù)據(jù)回傳,一個(gè)Activity可以傳遞給多個(gè)Activity,接收多個(gè)Activity返回的數(shù)據(jù)