activity传递数据

时间:2023-12-25 11:30:13

这些都是老生常谈了,到处都搜的到,但是因为经常忘记,放着好调用:

传递数据:

Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putString("name", nameStr);
bundle.putDouble("height", heightNum);
intent.putExtras(bundle);
startActivity(intent);

接收数据:

Bundle bundle = this.getIntent().getExtras();

String name = bundle.getString("name");

activity返回数据:

Intent intent = new Intent(a1.this,a2.class);
startActivityForResult(intent,1);

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
switch(resultCode)
{
case (RESULT_OK) : //有数据返回刷新
{
Bundle b=data.getExtras();
String postion=b.getString("position");
}break;
case (RESULT_CANCELED) :
{
}break;
}
}

Intent intent = getIntent();
Bundle bundle=new Bundle();
bundle.putString("position", postion);
intent.putExtras(bundle);
setResult(RESULT_OK, intent); finish();