關於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);
---------------------
作者:淼淼1111
來源:CSDN
原文:https://blog.csdn.net/u010112268/article/details/83832021
版權聲明:本文為博主原創文章,轉載請附上博文鏈接!


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM