From your FirstActivity call the SecondActivity using startActivityForResult() method
Code:
Intent i = new Intent(this, SecondActivity.class);
startActivityForResult(i, 1);
In your SecondActivity set the data which you want to return back to FirstActivity.
eg: In SecondActivity if you want to send back data set a onclicklistener on the item and use this code:
Intent returnIntent = new Intent();
returnIntent.putExtra("image",image);
returnIntent.putExtra("title",title);
setResult(RESULT_OK,returnIntent);
finish();
If the user doesn't select any list item and pressed Back Button.
In your MainActivity use this code.
@Override
public void onBackPressed() {
Intent returnIntent = new Intent();
setResult(RESULT_CANCELED, returnIntent);
finish();
}
Now in your FirstActivity class write following code for onActivityResult() method
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
if(resultCode == RESULT_OK){
String image=data.getIntExtra("result");
String title=data.getStringExtra("title");
ImageView img = (ImageView) findViewById(R.id.imageid);
TextView txt = (TextView) findViewById(R.id.txtid);
image.setImageResource(image);
txt.setText(title);
}
if (resultCode == RESULT_CANCELED) {
//Write your code if there's no result
}
}
}//onActivityResult