使用Bundle在Activity间传递数据

[b]使用Bundle 在Activity 间传递数据1:[/b]

[b]1.1从源Activity 中传递数据[/b]
//数据写入Intent
[i][color=darkblue]Intent openWelcomeActivityIntent=new Intent();
Bundle myBundelForName=new Bundle();
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);[/color][/i]

[b]1.2目标Activity 中获取数据[/b]
//从Intent 中获取数据
[i][color=darkblue]Bundle myBundelForGetName=this.getIntent().getExtras();
String name=myBundelForGetName.getString("Key_Name");
myTextView_showName.setText("欢迎您进入:"+name);[/color][/i]

[b]使用Bundle 在Activity 间传递数据2:[/b]
2.1从源请求Activity 中通过一个Intent 把一个服务请求传到目标Activity 中
//从Intent 中获取数据
[color=darkblue][i]Bundle myBundelForGetName=this.getIntent().getExtras();
String name=myBundelForGetName.getString("Key_Name");
myTextView_showName.setText("欢迎您进入:"+name);
private Intent toNextIntent;//Intent 成员声明
toNextIntent=new Intent();//Intent 定义
toNextIntent.setClass(TwoActivityME3.this, SecondActivity3.class);[/i][/color]
//设定开启的下一个Activity
[color=darkblue][i]startActivityForResult(toNextIntent, REQUEST_ASK););[/color][/i]

2.2开启Intent 时候,把请求码同时传递在源请求Activity 中等待Intent 返回应答结果,通过重载onActivityResult()方法
[color=darkblue][i]@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_ASK){
if(resultCode==RESULT_CANCELED){
setTitle("Cancel****");
}else if(resultCode==RESULT_OK){
showBundle=data.getExtras();//从返回的Intent中获得Bundle
Name=showBundle.getString("myName");//从bundle中获得相应数据
text.setText("the name get from the second layout:\n"+Name);
}
}
}
[/color][/i]
☻ 第一个参数是你开启请求Intent时的对应请求码,可以自己定义。
☻ 第二个参数是目标Activity返回的验证结果码
☻ 第三个参数是目标Activity返回的Intent
2.3目标Activity 中发送请求结果代码,连同源Activity 请求的数据一同绑定到Bundle
中通过Intent 传回源请求Activity 中
[color=darkblue][i]backIntent=new Intent();
stringBundle=new Bundle();
stringBundle.putString("myName", Name);
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);//返回Activity结果码
finish(););[/color][/i]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 在Android的Activity中,可以使用Bundle对象来将数据递给其他Activity。在发送Activity中,使用putExtra()方法将数据放入Bundle对象中,再递给目标Activity。在目标Activity中,使用getExtras()方法获取Bundle对象,从而获取数据。可以根据数据的类型使用不同的getXXX()方法获取对应的类型数据。 ### 回答2: Bundle是Android中一个常用的数据递方式,它是一种键对的数据结构,可以存储各种数据类型,如字符串、整数、布尔等。在Android中,我们经常需要在不同的Activity传递数据,而使用Bundle可以简单有效地实现这一目的。下面将详细介绍如何使用BundleActivity交换数据。 1.将数据放入Bundle递 在要传递数据Activity中,可以通过以下代码将数据放入Bundle中: ``` //创建一个Bundle对象 Bundle bundle = new Bundle(); //封装数据 bundle.putString("key1","value1"); bundle.putInt("key2", 2); bundle.putBoolean("key3", true); //将bundle递给另一个Activity Intent intent = new Intent(MainActivity.this,TargetActivity.class); intent.putExtras(bundle); startActivity(intent); ``` 在上面的代码中,我们首先创建了一个Bundle对象,并使用putString、putInt、putBoolean等方法将数据封装到Bundle对象中。然后通过Intent将Bundle对象递给需要接收数据的Activity。 2.在另一个Activity中获取数据 接收数据的Activity可以通过以下代码获取递过来的数据: ``` //获取intent中的Bundle对象 Bundle bundle = getIntent().getExtras(); //从bundle中取出数据 String value1 = bundle.getString("key1"); int value2 = bundle.getInt("key2"); boolean value3 = bundle.getBoolean("key3"); ``` 首先我们通过getIntent().getExtras()方法获取到Intent中递过来的Bundle对象。然后我们可以使用getString、getInt、getBoolean等方法从Bundle中取出我们需要的数据。 在使用Bundle进行数据递时,需要注意以下几点: 1. 要确保key传递数据时一致,否则会导致数据无法递或获取不到。 2. Bundle对象只能用于在两个Activity传递数据,不适用于跨进程的数据递。 3. 在存储数据时,要注意数据的类型与大小,尤其是数据量较大时,需要考虑使用其他方式,如SharedPreferences、SQLite等存储数据。 综上所述,Bundle是Android中一种简单而有效的数据递方式,在进行Activity的数据递时,可以优先考虑使用Bundle。 ### 回答3: Bundle是Android中用于输数据的一种数据结构,使用Bundle可以轻松地在Activity输数据。下面是使用BundleActivity交换数据的示例: 1. 在要发送数据的活动中,创建一个Bundle对象并将需要递的数据添加到该对象中。例如,我们要将用户名和密码递给另一个活动: ``` String username = "John"; String password = "123456"; Bundle bundle = new Bundle(); bundle.putString("username", username); bundle.putString("password", password); ``` 2. 启动要接收数据的活动,并将Bundle对象递给该活动。以下是一个例子: ``` Intent intent = new Intent(this, ReceiveActivity.class); intent.putExtras(bundle); startActivity(intent); ``` 3. 在接收数据的活动中,获取递的数据并将其用于需要的操作。例如,我们可以在接收活动中检索用户名和密码的: ``` Bundle extras = getIntent().getExtras(); if (extras != null) { String username = extras.getString("username", ""); String password = extras.getString("password", ""); } ``` 注意:使用Bundle传递数据时,存储的数据类型应该是Android支持的数据类型,如String、int、float等。  综上所述,使用BundleActivity交换数据非常方便可靠,这种方法适用于需要递大量数据或递复杂数据结构的场景,可以大大简化Android编程的工作量。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值