1.向下一个activity传递数据
activity1
1 Button button=(Button) findViewById(R.id.button1); 2 button.setOnClickListener(new OnClickListener() { 3 4 @Override 5 public void onClick(View v) { 6 Intent intent=new Intent("1111111111111111111"); 7 intent.addCategory("android.intent.category.MY_CATEGORY"); 8 intent.putExtra("aaa", "this is a message"); 9 startActivityForResult(intent, 0x11); 10 } 11 });
activity2
1 Intent intent=getIntent(); 2 String data=intent.getStringExtra("aaa"); 3 Toast.makeText(activity2.this, data, Toast.LENGTH_SHORT).show();
2.返回数据给上一个activity
activity2,按钮实现返回到activity1
1 Button jumpButton=(Button) findViewById(R.id.jumpButton); 2 jumpButton.setOnClickListener(new OnClickListener() { 3 @Override 4 public void onClick(View v) { 5 Intent intent=new Intent(); 6 intent.putExtra("xxx", "This is second activity data!"); 7 setResult(RESULT_OK,intent); 8 finish(); 9 } 10 });
activity1
重写onActivityResult()
1 @Override 2 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 4 super.onActivityResult(requestCode, resultCode, data); 5 if (requestCode==0x11) { 6 if (resultCode==RESULT_OK) { 7 returnData=data.getStringExtra("xxx"); 8 } 9 } 10 }
时间: 2024-11-08 05:38:46