1. 程式人生 > >關於Android Activity之間傳遞資料的6種方式

關於Android Activity之間傳遞資料的6種方式

使用Inten的putExtra傳遞

第一個Activity中

?

1

2

3

4

5

6

//建立意圖物件

 Intent intent = new Intent(this,TwoActivity.class);

 //設定傳遞鍵值對

 intent.putExtra("data",str);

 //啟用意圖

 startActivity(intent);

第二個Activity中

?

1

2

3

4

5

6

// 獲取意圖物件

 

Intent intent = getIntent();

 //獲取傳遞的值

 String str = intent.getStringExtra("data");

 //設定值

 tv.setText(str);

使用Intention的Bundle傳遞

第一個Activity中

?

1

2

3

4

5

6

7

8

9

//建立意圖物件

 Intent intent = new Intent(MainActivity.this,TwoActivity.class);

 

//用資料捆傳遞資料

 Bundle bundle = new Bundle();

 bundle.putString("data", str);

 //把資料捆設定改意圖

 intent.putExtra("bun", bundle);

 //啟用意圖

 startActivity(intent);

第二個Activity

?

1

2

3

4

5

//獲取Bundle

 Intent intent = getIntent();

 Bundle bundle = intent.getBundleExtra(

"bun");

 String str = bundle.getString("data");

 tv.setText(str);

使用Activity銷燬時傳遞資料

第一個Activity中

?

1

2

3

4

5

6

7

8

9

10

  Intent intent = new Intent(MainActivity.this,TwoActivity.class);

  //用一種特殊方式開啟Activity

 startActivityForResult(intent, 11);

//設定資料

  

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

 super.onActivityResult(requestCode, resultCode, data);

 String str = data.getStringExtra("data");

 tvOne.setText(str);

}

第二個activity中

?

1

2

3

4

5

6

//設定返回的資料

 Intent intent = new Intent();

 intent.putExtra("data", edtOne.getText().toString().trim());

 setResult(3, intent);

 //關閉當前activity

 finish();

SharedPreferences傳遞資料

第一個Activity中

?

1

2

3

4

5

6

7

8

9

SharedPreferences sp = this.getSharedPreferences("info", 1);

 //獲取sp編輯器

 Editor edit = sp.edit();

 edit.putString("data", str);

 edit.commit();

 //建立意圖物件

 Intent intent = new Intent(MainActivity.this,TwoActivity.class);

 //啟用意圖

 startActivity(intent);

第二個Activity中

?

1

2

3

SharedPreferences sp = this.getSharedPreferences("info", 1);

 //設定資料

 tv.setText(sp.getString("data", ""));

使用序列化物件Seriazable

工具類

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

import java.io.Serializable;

class DataBean implements Serializable {

 private String name;

 private String sex;

 public String getName() {

 return name;

 }

 public void setName(String name) {

 this.name = name;

 }

 public String getSex() {

 return sex;

 }

 public void setSex(String sex) {

 this.sex = sex;

 }

}

第一個Activity

?

1

2

3

4

5

6

7

8

//建立意圖

 Intent intent = new Intent(MainActivity.this,TwoActivity.class);

 DataBean bean = new DataBean();

 //通過set方法把資料儲存到DataBean物件中

 bean.setName("啦啦");

 bean.setSex("男");

 intent.putExtra("key", bean);

 startActivity(intent);

第二個Activity

?

1

2

3

4

5

6

7

8

Intent intent = getIntent();

 //反序列化資料物件

 Serializable se = intent.getSerializableExtra("key");

 if(se instanceof DataBean){

  //獲取到攜帶資料的DataBean物件db

  DataBean db = (DataBean) se;

  tv.setText(db.getName()+"==="+db.getSex());

 }

使用靜態變數傳遞資料

第一個Activity

?

1

2

3

4

Intent intent = new Intent(MainActivity.this,TwoActivity.class);

  TwoActivity.name="牛逼";

  TwoActivity.str="你說";

  startActivity(intent);

第二個Activity

?

1

2

3

4

//靜態變數

protected static String name;

protected static String str;

tv.setText(str+name);