Reputation:
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);
}
Upvotes: 0
Views: 2595
Reputation: 4639
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.
Upvotes: 1
Reputation: 9507
Get your First Activity intent in Acitivity2.java
and again pass it to Activity3.java
Activity1.java
Intent intent = new Intent (this, Activity2.class);
intent.putExtra(konto_name1, name);
startActivity(intent);
Activity2.java
Intent intent = new Intent (this, Activity3.class);
final String firstActivityValue = getIntent().getStringExtra("name");
intent.putExtra(start_value1, value);
intent.putExtra(start_value2, firstActivityValue);
startActivity(intent);
Now you can get both values in Activity3.java
using getIntent().
Hope it helps.
Upvotes: 1
Reputation: 2528
Modify your second activity code like this. See in second activity you have a different intent. It has no relation between the intent from 1st activity. So you need to take the value from 1st activity intent to 2nd activity intent.
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);
intent.putExtra(konto_name1,getIntent().getStringExtra(konto_name1));//Add this line in your code
startActivity(intent);
}
Upvotes: 1