主要是传递自定义对象
窗体A
窗体B
窗体A调用窗体B,B确定后回调A方法
MainActivity
public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent in=new Intent(MainActivity.this,Activity1.class); Bundle bundle=new Bundle(); //传递boolean bundle.putBoolean("boolean", true); bundle.putStringArray("strings", new String[]{"苹果","鸭梨","香蕉"}); ArrayList<HashMap<String, Object>> usersMap = new ArrayList<HashMap<String, Object>>(); for(int i=0;i<2;i++){ HashMap<String, Object> a=new HashMap<String, Object>(); a.put("id", i); a.put("user"+i, new User("XXX"+i,i)); usersMap.add(a); } bundle.putSerializable("usersMap", usersMap); ArrayList<User> users=new ArrayList<User>(); for(int i=0;i<2;i++){ User user=new User("XXX"+i,i); users.add(user); } bundle.putSerializable("users", users); User user=new User("XXX",1); bundle.putSerializable("user", user); in.putExtras(bundle); startActivityForResult(in, 100); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if((requestCode==100)&&(resultCode==100)){ Toast.makeText(MainActivity.this, data.getStringExtra("msg"), Toast.LENGTH_SHORT).show(); } } }
Activity1
public class Activity1 extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_activity1); Intent in=getIntent(); Bundle ble=in.getExtras(); EditText et=(EditText)findViewById(R.id.editText1); String str="boolean:"+ble.getBoolean("boolean")+" "; str=str+"数组:"+ble.getStringArray("strings")[0].toString()+" "; ArrayList<HashMap<String, Object>> usersMap=(ArrayList<HashMap<String, Object>>)ble.getSerializable("usersMap"); str=str+"Map的List:"+usersMap.toString()+" "; ArrayList<User> users=(ArrayList<User>)ble.getSerializable("users"); str=str+"User的List:"+users.toString()+" "; User user=(User)ble.getSerializable("user"); str=str+"对象:"+user.toString()+" "; et.setText(str); Button btn=(Button)findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent in=new Intent(); in.putExtra("msg", "成功222"); setResult(100,in); finish(); } }); } }