在使用新浪微博APP时,能发现在微博发布界面进入图库选择图片后,会回到微博发布页面并带回了图片选择页面的图片信息。由于这种需求十分常见,因此,Android提供了一个startActivityForResult()方法,来实现回传数据。
要求:页面1跳转到页面2,页面2再返回页面1同时返回数据
页面1添加如下代码:
Intent intent = new Intent(); intent.setClass(页面1.this, 页面2.class); Bundle bundle = new Bundle(); intent.putExtras(bundle);//将Bundle添加到Intent,也可以在Bundle中添加相应数据传递给下个页面,例如:bundle.putString("abc", "bbb"); startActivityForResult(intent, 0);// 跳转并要求返回值,0代表请求值(可以随便写)
页面2接收数据添加代码如下:
Intent intent = this.getIntent(); Bundle bundle = intent.getExtras(); bundle.putString("aaa", "back");//添加要返回给页面1的数据 intent.putExtras(bundle); this.setResult(Activity.RESULT_OK, intent);//返回页面1 this.finish();
页面1接收返回数据:(需要重写onActivityResult)
onActivityResult()方法里面的三个参数 第一个参数requestCode,表示再启动Acitivity是传递的请求码 第二个参数resultCode,表示再返回数据时传入结果码 第三个参data,表示携带返回数据的Intent
@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 0 && resultCode == Activity.RESULT_OK) { Bundle bundle = data.getExtras(); gameView.backString = bundle.getString("aaa"); Toast.makeText(this, backString, Toast.LENGTH_SHORT).show(); } }
我用一个具体的案例更好的帮助大家理解下startActivityForResult()方法
首先看一下效果图
运行程序,在主界面中分别单机“主人购买装备” “小宝宝购买装备” 按钮 , 会跳转至装备展示界面(第二幅图),装备购买成功后,会看到第三幅图片。
首先创建一个ItemInfo的类 用于封装装备信息。具体代码如下:
public class ItemInfo implements Serializable {
private String name;
private int acctack;
private int life;
private int speed;
public ItemInfo(String name, int acctack, int life, int speed) {
this.name = name;
this.acctack = acctack;
this.life = life;
this.speed = speed;
}
public String getName() {
return name;
}
public int getAcctack() {
return acctack;
}
public int getLife() {
return life;
}
public int getSpeed() {
return speed;
}
public void setName(String name) {
this.name = name;
}
public void setAcctack(int acctack) {
this.acctack = acctack;
}
public void setLife(int life) {
this.life = life;
}
public void setSpeed(int speed) {
this.speed = speed;
}
@Override
public String toString() {
return "ItemInfo{" +
"name='" + name + '\'' +
", acctack=" + acctack +
", life=" + life +
", speed=" + speed +
'}';
}
}
创建一个ShopActivity是用来展示装备信息的,当单击ShopActivity的装备时,会调用MainActicity并将装备信息回调给MainActivity,ShopActivity的具体代码如下:
public class ShopActivity extends Activity implements View.OnClickListener{
private ItemInfo mItemInfo;
private TextView mLifeTv;
private TextView mNameTv;
private TextView mSpeedTv;
private TextView mAttackTv;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shop);
mItemInfo = new ItemInfo("金剑",100,20,20);
findViewById(R.id.rl).setOnClickListener(this);
initWidget();
}
private void initWidget() {
mLifeTv = (TextView) findViewById(R.id.tv_life);
mNameTv = (TextView) findViewById(R.id.tv_name);
mSpeedTv = (TextView) findViewById(R.id.tv_speed);
mAttackTv = (TextView) findViewById(R.id.tv_attack);
mLifeTv.setText("生命值"+ mItemInfo.getLife());
mNameTv.setText(mItemInfo.getName()+"");
mSpeedTv.setText("敏捷度"+ mItemInfo.getSpeed());
mAttackTv.setText("攻击力"+mItemInfo.getAcctack());
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.rl:
Intent intent = new Intent();
intent.putExtra("equipment",mItemInfo);
setResult(1,intent);
finish();
break;
}
}
}
public class MainActivity extends AppCompatActivity {
private TextView mLifeTv;
private TextView mAttackTv;
private TextView mSpeedTv;
private ProgressBar mProgressBar1;
private ProgressBar mProgressBar2;
private ProgressBar mProgressBar3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initWidget();
}
private void initWidget() {
mLifeTv = (TextView) findViewById(R.id.tv_life_progress);
mAttackTv = (TextView) findViewById(R.id.tv_attack_progress);
mSpeedTv = (TextView) findViewById(R.id.tv_speed_progress);
mProgressBar1 = (ProgressBar) findViewById(R.id.progressBar1);
mProgressBar2 = (ProgressBar) findViewById(R.id.progressBar2);
mProgressBar3 = (ProgressBar) findViewById(R.id.progressBar3);
mProgressBar1.setMax(100); // 设置最大值为100
mProgressBar2.setMax(100);
mProgressBar3.setMax(100);
}
public void click(View view){
Intent intent = new Intent(this,ShopActivity.class);
startActivityForResult(intent,1); // 返回请求结果返回值为1
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null){
//判断结果码是否等于1 , 等于1为宝宝添加装备
if (resultCode == 1){
if (requestCode == 1){
ItemInfo info = (ItemInfo) data.getSerializableExtra("equipment");
// 更新ProgressBar的值
updatePogress(info);
}
}
}
}
private void updatePogress(ItemInfo info) {
int progress1 = mProgressBar1.getProgress();
int progress2 = mProgressBar2.getProgress();
int progress3 = mProgressBar3.getProgress();
mProgressBar1.setProgress(progress1+info.getLife());
mProgressBar2.setProgress(progress2+info.getAcctack());
mProgressBar3.setProgress(progress3+info.getSpeed());
mLifeTv.setText(mProgressBar1.getProgress()+"");
mAttackTv.setText(mProgressBar2.getProgress()+"");
mSpeedTv.setText(mProgressBar3.getProgress()+"");
}
}
activity_main.xml如下:
装备界面的xml如下:activity_shop.xml