activity传递数据

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

传递数据:

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();

上一篇:python制作命令行工具——fire


下一篇:Python--day05(数字、字符串、列表)