如何将数据从一个活动(意图)发送到另一个活动?
我使用此代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
如何将数据从一个活动(意图)发送到另一个活动?
我使用此代码发送数据:
Intent i=new Intent(context,SendMessage.class);
i.putExtra("id", user.getUserAccountId()+"");
i.putExtra("name", user.getUserFullName());
context.startActivity(i);
当前回答
首先,使用getIntent()方法获取开始活动的意图:
Intent intent = getIntent();
如果额外的数据表示为字符串,则可以使用intent.getStringExtra(字符串名称)方法。在您的案例中:
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
其他回答
在接收活动中
Bundle extras = getIntent().getExtras();
String userName;
if (extras != null) {
userName = extras.getString("name");
// and get whatever type user account id is
}
如果在FragmentActivity中使用,请尝试以下操作:
第一页扩展了FragmentActivity
Intent Tabdetail = new Intent(getApplicationContext(), ReceivePage.class);
Tabdetail.putExtra("Marker", marker.getTitle().toString());
startActivity(Tabdetail);
在片段中,您只需要首先调用getActivity(),
第二页扩展片段:
String receive = getActivity().getIntent().getExtras().getString("name");
你也可以这样做//将价值放在意图上
Intent in = new Intent(MainActivity.this, Booked.class);
in.putExtra("filter", "Booked");
startActivity(in);
//从意图中获得价值
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String filter = bundle.getString("filter");
加起来
设置数据
String value = "Hello World!";
Intent intent = new Intent(getApplicationContext(), NewActivity.class);
intent.putExtra("sample_name", value);
startActivity(intent);
获取数据
String value;
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
value = bundle.getString("sample_name");
}
不要初始化另一个新的Intent来接收数据,只需执行以下操作:
String id = getIntent().getStringExtra("id");