You have mixed requestCode
and resultCode
. resultCode
should be one of predefined constants RESULT_CANCELED
, RESULT_OK
, RESULT_FIRST_USER
etc. requestCode
is your unique values - 50, 100, etc.
Also the requestCode
should be the same in the startActivityForResult
call and in onActivityResult
handler in the same activity.
For example, in the SongActivity you should call:
setResult(RESULT_OK, in3);
and in the ArtistActivity you should call:
startActivityForResult(in2, 50);
because your onActivityResult
in the ArtistActivity awaits code 50.
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent in2 = new Intent(getApplicationContext(), SongActivity.class);
// Starting SongActivity and wait for results with requestCode 50
startActivityForResult(in2, 50);
}
});
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 50 && resultCode == RESULT_OK)
{
// get results from SongActivity (requestCode = 50)
songIndexArtist = data.getExtras().getInt("songIndex");
// now pass the same data into MainActivity
Intent result = new Intent();
result.putExtra("songIndexArtist", songIndexArtist);
setResult(RESULT_OK, result);
finish();
}
}
Note that I replaced resultCode with requestCode.
You should fix MainActivity in the similar way.
Also you may possibly use FLAG_ACTIVITY_FORWARD_RESULT
flag; there is a related SO question and a good explanation of this flag (among other flags). The flag is usefull if you pass results back through intermediate activities without any processing, "as is".