使用bundle在activity間傳遞資料

2021-06-27 08:44:27 字數 3488 閱讀 4035

1.1從源activity 中傳遞資料 1

2

3

4

5

6

7

intent openwelcomeactivityintent=newintent();

bundle mybundelforname=newbundle();

mybundelforname.putstring("key_name",inname.gettext().tostring());

mybundelforname.putstring("key_age",inage.gettext().tostring());

openwelcomeactivityintent.putextras(mybundelforname);

openwelcomeactivityintent.setclass(androidbundel.this, welcome.class);

startactivity(openwelcomeactivityintent);

1.2目標activity 中獲取資料 1

2

3

4

//從intent 中獲取資料

bundle mybundelforgetname=this.getintent().getextras();

string name=mybundelforgetname.getstring("key_name");

mytextview_showname.settext("歡迎您進入:"+name);

使用bundle 在activity 間傳遞資料2:

2.1從源請求activity 中通過乙個intent 把乙個服務請求傳到目標activity 中

1

2

3

4

5

6

7

8

9

//從intent 中獲取資料

bundle mybundelforgetname=this.getintent().getextras();

string name=mybundelforgetname.getstring("key_name");

mytextview_showname.settext("歡迎您進入:"+name);

privateintent tonextintent;//intent 成員宣告

tonextintent=newintent();//intent 定義

tonextintent.setclass(twoactivityme3.this, secondactivity3.class);

//設定開啟的下乙個activity

startactivityforresult(tonextintent, request_ask););

2.2開啟intent 時候,把請求碼同時傳遞在源請求activity 中等待intent 返回應答結果,通過過載onactivityresult()方法

1

2

3

4

5

6

7

8

9

10

11

12

13

@override

protectedvoidonactivityresult(intrequestcode,intresultcode,intent data)elseif(resultcode==result_ok)

}

}

☻ 第乙個引數是你開啟請求intent時的對應請求碼,可以自己定義。 

☻ 第二個引數是目標activity返回的驗證結果碼 

☻ 第三個引數是目標activity返回的intent 

2.3目標activity 中傳送請求結果**,連同源activity 請求的資料一同繫結到bundle 

中通過intent 傳回源請求activity 中

1

2

3

4

5

6

backintent=newintent();

stringbundle=newbundle();

stringbundle.putstring("myname", name);

backintent.putextras(stringbundle);

setresult(result_ok, backintent);//返回activity結果碼

finish();

使用Bundle在Activity間傳遞資料

文章 1 使用bundle在activity間傳遞資料1 從源activity 中傳遞資料 資料寫入intent intent openwelcomeactivityintent new intent bundle mybundelforname new bundle mybundelforname...

使用Bundle在Activity之間傳遞資料

bundle可能過put 方法新增各種型別的資料,intent也可以通過putextras bundle 將資料新增進去,然後通過startactivity 跳到下一下activity的時候就把資料也傳到下乙個activity了。package com.intent import android.c...

Bundle物件的使用

在android開發中,如果要通過乙個activity啟動另外乙個activity,需要呼叫startactivity 函式,這個函式的引數是乙個intent物件,這個物件通常的初始化方式如下 intent intent new intent intent.setclass this,seconda...