在android开发过程中,会经常用到activity与activity之间数据传递的问题,若是有多个数据需要传递的话,可以考虑使用Bundle,下面就简单介绍下Bundle如何在activity之间传递数据。
注:如下例子工程,MainActivity.java 是主activity
(一)首先,假如要从MainActivity.java 向 Test.java 传递数据,看下面例子:
MainActivity.java
package com.lianxi.slidingdrawer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//数据写入Intent
Intent openWelcomeActivityIntent=new Intent();
Bundle myBundelForName=new Bundle();
myBundelForName.putString("Key_Name","名字");
myBundelForName.putString("Key_Age","年龄");
openWelcomeActivityIntent.putExtras(myBundelForName);
openWelcomeActivityIntent.setClass(MainActivity.this, Test.class);
startActivity(openWelcomeActivityIntent);
}
}
Test.java
package com.lianxi.slidingdrawer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class Test extends Activity{
private static final String TAG = "Test";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle myBundelForGetName = this.getIntent().getExtras();
String name = myBundelForGetName.getString("Key_Name");
String age = myBundelForGetName.getString("Key_Age");
Log.e("TAG", "name= "+name+" age= " + age);
}
}
代码分析:
从源Activity (MainActivity.java) 中传递数据:
//数据写入Intent
Intent openWelcomeActivityIntent=new Intent();
Bundle myBundelForName=new Bundle();
myBundelForName.putString("Key_Name","名字");
myBundelForName.putString("Key_Age","年龄");
openWelcomeActivityIntent.putExtras(myBundelForName);
openWelcomeActivityIntent.setClass(AndroidBundel.this, Welcome.class);
startActivity(openWelcomeActivityIntent);
目标Activity (Test.java) 中获取数据:
//从Intent 中获取数据
Bundle myBundelForGetName=this.getIntent().getExtras();
String name=myBundelForGetName.getString("Key_Name");
myTextView_showName.setText("欢迎您进入:"+name);
(二)再就是,假如要从MainActivity.java 跳转到 Test.java ,若Test.java 的运行结果是ok的话,则给MainActivity.java 传递数据,看下面例子:
MainActivity.java
package com.lianxi.slidingdrawer;
import android.os.Bundle;
import android.util.Log;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
private static final String TAG = "MainActivity";
private Intent toNextIntent;//Intent 成员声明
private int REQUEST_ASK = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
toNextIntent=new Intent();//Intent 定义
toNextIntent.setClass(MainActivity.this, Test.class);
//设定开启的下一个Activity
startActivityForResult(toNextIntent, REQUEST_ASK);//开启Intent 时候,把请求码同时传递
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==REQUEST_ASK){
if(resultCode==RESULT_CANCELED){
setTitle("Cancel****");
}else if(resultCode==RESULT_OK){
Bundle showBundle=data.getExtras();//从返回的Intent中获得Bundle
String Name = showBundle.getString("myName");//从bundle中获得相应数据
Log.e("name", Name);
}
}
}
}
Test.java
package com.lianxi.slidingdrawer;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Test extends Activity{
private static final String TAG = "Test";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Intent backIntent=new Intent();
Bundle stringBundle=new Bundle();
stringBundle.putString("myName", "名字");
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);//返回Activity结果码
finish();
}
}
代码分析:
从源请求Activity(MainActivity.java) 中通过一个Intent 把一个服务请求传到目标Activity(Test.java) 中
private Intent toNextIntent;//Intent 成员声明
toNextIntent=new Intent();//Intent 定义
toNextIntent.setClass(TwoActivityME3.this, SecondActivity3.class);
//设定开启的下一个Activity
startActivityForResult(toNextIntent, REQUEST_ASK);
//开启Intent 时候,把请求码同时传递
在源请求Activity(MainActivity.java) 中等待Intent 返回应答结果,通过重载onActivityResult()方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
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);
}
}
}
☻ 第一个参数是你开启请求Intent时的对应请求码,可以自己定义。
☻ 第二个参数是目标Activity返回的验证结果码
☻ 第三个参数是目标Activity返回的Intent
目标Activity(Test.java) 中发送请求结果代码,连同源Activity(MainActivity.java) 请求的数据一同绑定到Bundle
中通过Intent 传回源请求Activity (MainActivity.java)中
backIntent=new Intent();
stringBundle=new Bundle();
stringBundle.putString("myName", Name);
backIntent.putExtras(stringBundle);
setResult(RESULT_OK, backIntent);//返回Activity结果码
finish();