Activity之间的数据传递_Intent和startActivityForResult的使用
本节主要讨论下Activity之间(A和B)的数据该如何传递。
1、A->B:A跳转到B的同时,将一些数据传递给B
代码实现:很简单,只需在A中生成Intent的时候,将需要传递的数据放到Intent中,如下:(1)A
Intent intent = new Intent();intent.setClass(A.this, B.class);Bundle bundle = new Bundle();bundle.putString("story", "Tom&jerry");intent.putExtra("tag", bundle);startActivity(intent); Intent intent = getIntent();Bundle bundle = intent.getBundleExtra("tag");String storyName = bundle.getString("story");startActivityForResult(intent, 0);
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {// TODO Auto-generated method stubswitch (resultCode) {case RESULT_OK:Bundle bundle = data.getBundleExtra("tag");String storyName = bundle.getString("story");tv1.setText(storyName);break;default:break;}}B.this.setResult(RESULT_OK,intent);即可实现返回A界面,并把数据传回给A,intent中的数据可以为A最开始传过来的数据,也可以是经过B处理过的数据。