[英]How do you pass a string through 2 activities?
I need to send an input from a user in one activity that sends it to activity2
and then activity2
sends the input from activity1
to activity3
. 我需要在一个活动中从用户发送输入,将其发送到
activity2
,然后activity2
将输入从activity1
发送到activity3
。 visually, it would go: 在视觉上,它会:
activity1
has an edit text field which sends the input to activity2
, activity2
uses that same input and sends it to activity3
. activity1
有一个编辑文本字段,用于将输入发送到activity2
, activity2
使用相同的输入并将其发送到activity3
。
(activity1) --String--> (activity2) --String--> (activity3)
Do I have to getIntent from activity1
and send it to activity3
from the onclick
method in activity2
? 我是否必须从
activity1
获取getIntent并将其从activity3
中的onclick
方法发送到activity2
?
You need to pass it as an extra to the second activity: 您需要将其作为额外的第二个活动传递给它:
String string = "whatever";
Intent i = new Intent(this, Activity2.class);
i.putExtra("somename", string);
startActivity(i);
Then get it in the second activity like this: 然后在第二个活动中得到它,如下所示:
Intent intent = getIntent();
String string = intent.getExtras().getString("somename");
You can then repeat that process in Activity2 (with some different variable names as to not confuse yourself later) to send from the second activity and get in Activity3 然后,您可以在Activity2中重复该过程(使用一些不同的变量名称以便以后不要混淆)从第二个活动发送并进入Activity3
String passString= "information im sending";
Intent intent = new Intent(this, secondActivity.class);
intent.putExtras("DataKey", passString);
startActivity(intent);
//in your second activity
Intent intent = getIntent();
String recieveString = intent.getStringExtra("DataKey");
//repeat the same in your second activity but this time the string will change
Intent intent = new Intent(this, thirdActivity.class);
intent.putExtras("DataKey", recieveString);
startActivity(intent);
//in your third activity
Intent intent = getIntent();
String recieveString2 = intent.getStringExtra("DataKey");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.