How to receive multiple values using an intent

android, java

Solution

You should use something like:

Intent returnIntent = new Intent();
returnIntent.putExtra("title",titleField.getText().toString());
returnIntent.putExtra("year",yearField.getText().toString());
setResult(RESULT_OK,returnIntent);     
finish(); 

And on your main activity, onActivityResult:

tempTitle = data.getStringExtra("title");
tempYear =  data.getStringExtra("year");

Problem

I have my main activity using the startActivityForResult method which calls an activity that i need to return two string values from. I have it working to return one, but even with all the tutorials and other questions on here i have read i cant seem to get it to return the two values. Below is my code. here is where i start the second activity: ``` Button button = (Button) findViewById(R.id.add); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivityForResult(addM, 1); } }); ``` Here is the activity it starts, i need to return the text that is in the titleField(which works now) and the yearField ``` public class AddMovie extends Activity { String movieTitle, movieYear; EditText titleField, yearField; Button save; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_add_movie); titleField = (EditText) findViewById(R.id.titleField); yearField = (EditText) findViewById(R.id.yearField); save = (Button) findViewById(R.id.saveMovie); save.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent data = new Intent(); data.setData(Uri.parse(titleField.getText().toString())); setResult(RESULT_OK, data); //data.setData(Uri.parse(yearField.getText().toString())); //setResult(RESULT_OK, data); finish(); } }); } } ``` Here is the method in my main class that receives results ``` public void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == request_Code) { if(resultCode == RESULT_OK) { tempTitle = data.getData().toString(); //tempYear = data.getStringExtra("movieYear"); Toast.makeText(this, tempTitle, Toast.LENGTH_SHORT).show(); dbAddMovie(tempTitle, tempYear); } } } ``` The code that is commented out was one attempt at making it receive multiple values, although they failed. Any help with this situation would be great. Thanks!

Original source