意向putExtra从两个活动 [英] Intent putExtra from two Activities
问题描述
您好,我希望你能帮助我:
我有三个活动:活性1 - >活性2 - > Activity3
在活动1和2我有一个意图,它把一个字符串从一个EditText额外费用。
而在Activity3我想收到两个字符串,但我尽量只Activity3收到活性2(在previous活动)。该意向
在这里,code从活动1:
公共无效的onClick(视图v){ 意向意图=新意图(这一点,Activity2.class);
ET1的EditText =(EditText上)findViewById(R.id.editText1);
字符串名称= et1.getText()的toString()。 如果(name.length()== 0){
新AlertDialog.Builder(本).setMessage(
R.string.error_name_missing).setNeutralButton(
R.string.error_ok,
NULL).show();
返回;
} intent.putExtra(konto_name1,名);
startActivity(意向); }
从活性2:
公共无效的onClick(视图v){ 意向意图=新意图(这一点,Activity3.class);
ET2的EditText =(EditText上)findViewById(R.id.editText2); 字符串值= et2.getText()的toString()。 如果(value.length()== 0){
新AlertDialog.Builder(本).setMessage(
R.string.error_value_missing).setNeutralButton(
R.string.error_ok,
NULL).show();
返回;
} intent.putExtra(start_value1,值);
startActivity(意向);
}
您可以刚刚从意图传递给意图,因为其他的答案讨论。然而,你也可以使用共享preferences
,使其更易于使用。
共享preferences preFS = getDefaultShared preferences(本);
编辑编辑= prefs.edit();
edit.putString(konto_name1,名)
edit.commit();
或者,如果你想它作为一个班轮:
preferenceManager.getDefaultShared preferences(本).edit()putString(konto_name1,名).commit();
然后,而不是从意向获取字符串,只需使用:
共享preferences preFS = preferenceManager.getDefaultShared preferences(本);
字符串newString = prefs.getString(konto_name1,);
这会做你想要什么,而不必从保持意图意图,这是我用来寻找恼人传递的字符串的赛道上最好的。
此外,上面只有一行:
preferenceManager.getDefaultShared preferences(本).getString(konto_name1,);
您应该做同样与活性2字符串。别担心,每次你的项目被点击活动时间(1/2)在preference将被覆盖。请注意,在的getString()
第二个参数是的情况下,默认的字符串要检索不存在的人。
Hello I hope you can help me:
I have three Activities: Activity1 --> Activity2 --> Activity3
In Activity1 and 2 I have an Intent, which put a string from an EditText Extra.
And in Activity3 I want to receive BOTH strings, but as I try Activity3 only receive the Intent from Activity2 (the previous activity).
Here the code from Activity1:
public void onClick(View v) {
Intent intent = new Intent (this, Activity2.class);
EditText et1 = (EditText) findViewById(R.id.editText1);
String name = et1.getText().toString();
if (name.length() == 0) {
new AlertDialog.Builder(this).setMessage(
R.string.error_name_missing).setNeutralButton(
R.string.error_ok,
null).show();
return;
}
intent.putExtra(konto_name1, name);
startActivity(intent);
}
From Activity2:
public void onClick(View v) {
Intent intent = new Intent (this, Activity3.class);
EditText et2 = (EditText) findViewById(R.id.editText2);
String value = et2.getText().toString();
if (value.length() == 0) {
new AlertDialog.Builder(this).setMessage(
R.string.error_value_missing).setNeutralButton(
R.string.error_ok,
null).show();
return;
}
intent.putExtra(start_value1, value);
startActivity(intent);
}
You can just pass from Intent to Intent, as the other answers discuss. However you can also use SharedPreferences
to make it easier to work with.
SharedPreferences prefs = getDefaultSharedPreferences(this);
Editor edit = prefs.edit();
edit.putString(konto_name1, name)
edit.commit();
Or if you wanted it as a one-liner:
PreferenceManager.getDefaultSharedPreferences(this).edit().putString(konto_name1, name).commit();
Then instead of getting the string from the Intent, just use:
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
String newString = prefs.getString(konto_name1, "");
That'll do what you want, without having to keep track of strings passed from Intent to Intent, which I used to find annoying at best.
Again, above as a one-liner:
PreferenceManager.getDefaultSharedPreferences(this).getString(konto_name1, "");
You should do the same with the String in Activity2. Don't worry, every time your item is clicked in Activity(1/2) the preference will be overwritten. Note that the second parameter in getString()
is a default String in case the one you want to retrieve doesn't exist.
这篇关于意向putExtra从两个活动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!