关于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);

以上所述是小编给大家介绍的关于Android Activity之间传递数据的6种方式,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对脚本之家网站的支持!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值