Activity跳轉(zhuǎn)(有返回?cái)?shù)據(jù)跳轉(zhuǎn))以及傳遞參數(shù)和回傳參數(shù)

使用Bundle在Activity之間傳遞數(shù)據(jù)

  • Bundle類是一個(gè)key-value對(duì),是一個(gè)final類
  • 兩個(gè)Activity之間通信可以用Bundle類實(shí)現(xiàn)步驟:
  1. 新建一個(gè)Bundle類
    Bundle bundle=new Bundle();
  2. bundle類中加入數(shù)據(jù)(key-value的形式)
    bundle.putString("data", "張三");
  3. 新建一個(gè)Intent對(duì)象,并將該Bundle加入該Intent對(duì)象
    Intent intent=new Intent();
    intent.setClass(TestBundle.this, Target.class);
    intent.putExtras(bundle);
  4. 啟動(dòng)Intent
    startActivity(intent);
  5. 第二個(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ù)

  • 步驟:
  1. 傳遞數(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);
  2. 接收數(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)行序列化)
  • 序列化的目的
  1. 永久的保存對(duì)象數(shù)據(jù)(將對(duì)象數(shù)據(jù)保存在文件當(dāng)中,或者是磁盤中)
  2. 通過(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é)流的形式)
  3. 將對(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ù)取出)
  4. 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ù)、或文件的形式保存)
  5. 序列化對(duì)象的時(shí)候只是針對(duì)變量進(jìn)行序列化,不針對(duì)方法進(jìn)行序列化.
  6. 在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ù)
    步驟:
  1. 頁(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)求值(可以隨便寫)

  1. 頁(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;

  1. 頁(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ù)
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 1、Intent 可以傳遞的數(shù)據(jù)類型 在 Intent和IntentFilters(1) 中我們知道了通過(guò) Int...
    CnPeng閱讀 2,980評(píng)論 0 10
  • ¥開(kāi)啟¥ 【iAPP實(shí)現(xiàn)進(jìn)入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開(kāi)一個(gè)線程,因...
    小菜c閱讀 7,311評(píng)論 0 17
  • 【 Android四大組件之一 主要用于與用戶進(jìn)行交互,在一個(gè)App中可能存在零個(gè)或多個(gè)Activity 】 1....
    征程_Journey閱讀 2,055評(píng)論 0 4
  • 文/江南風(fēng)景 我是喜歡看書的,記得很小的時(shí)候,我就買了紅樓夢(mèng),并且小學(xué)三年級(jí)時(shí)就把同桌父親的一本西游記看完了,那...
    江南風(fēng)景打工者的正能量閱讀 364評(píng)論 0 1
  • 今天下午,在往茂縣的客車上看了一部老電影——《新龍門客棧》。 塞外、大漠、客棧、俠客、義士,猛然一...
    君子溫和閱讀 1,365評(píng)論 0 0

友情鏈接更多精彩內(nèi)容