0

I am working on a mediaPlayer app for which I am using ListViews for my playlist. I have the main activity which has the mediaPlayer and when user clicks on the the playlist button, ArtistActivity is launch from which the user can launch SongActivity. I have looked at numerous tutorials and examples but for some reason SongActivity is passing "0" back to the main activity and playing the first track in the ArrayList with the songs URLs. Any help would be much appreciated. Please help. Thank you.

MainActivity:

    btnPlaylist.setOnClickListener(new View.OnClickListener() {


        public void onClick(View arg0) {
            Intent i = new  Intent(getApplicationContext(),ArtistActivity.class);
            startActivityForResult(i, 100);

        }
    });

}


protected void onActivityResult(int requestCode,
        int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == 50)
    {

        currentSongIndex = data.getExtras().getInt("songIndexArtist");

        songIndex = currentSongIndex;

        // play selected song
        playSong(currentSongIndex);

        // Displaying Song title
        String songTitle =
  songsList.get(currentSongIndex).get("songTitle");
        songTitleLabel.setText(songTitle);

    }

ArtistActivity:

lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {

            //int artistIndex = position;

            // Starting new intent
            Intent in2 = new Intent(getApplicationContext(),SongActivity.class);

            startActivityForResult(in2,100);

            setResult(100,in2);

            in2.putExtra("songIndexArtist", songIndexArtist);

            finish();

        }
    });
}
protected void onActivityResult(int requestCode,
        int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        if(resultCode == 50)
    {

        songIndexArtist = data.getExtras().getInt("songIndex");

    }

}

SongActivity:

lv.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {


            Toast.makeText(getApplicationContext(), "DOWNLOADING, PLEASE WAIT", Toast.LENGTH_LONG).show(); 

            // getting listitem index
            int songIndex = position;

            // Starting new intent
            Intent in3 = new Intent(getApplicationContext(),
                    AndroidBuildingMusicPlayerActivity.class);

            // Sending songIndex to PlayerActivity
            in3.putExtra("songIndex", songIndex);

            setResult(50,in3);

            // Closing PlayListView
            finish();
        }
    });
}
Ram kiran Pachigolla
  • 20,897
  • 15
  • 57
  • 78
TitaniuM
  • 321
  • 1
  • 10
  • 19

1 Answers1

2

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".

Community
  • 1
  • 1
Stan
  • 8,683
  • 9
  • 58
  • 102
  • Thank you Stan I fixed the requestcode and resultcode mix up but for some reason ArtistActivity is still passing "0" to MainActivity. I tried using Log.i to debug but I cant figure out where its getting "0" from. Log.i(getPackageName(), "result code in mainActivity: "+resultCode); Log.i(getPackageName(), "request code in mainActivity: "+requestCode); – TitaniuM Dec 27 '12 at 01:11
  • I double checked that in the SongActivity class, position is corresponding to which ever row i am selecting but when its passed back to MainActivity, it changes to "0". – TitaniuM Dec 27 '12 at 01:35
  • Did you solve the problem? Make sure that in **ArtistActivity** you moved the lines `setResult(RESULT_OK, result); result.putExtra("songIndexArtist", songIndexArtist); finish();` into `onActivityResult` in responce to finished **SongActivity**. Note that `result` must be a new intent: `Intent result = new Intent();` allocated specifically for passing data back to main activity - don't mix it with the intent used to invoke **SongActivity**. I've updated the answer. If the problem persists, you should possibly post updated code to make it easier to get current situation. – Stan Dec 27 '12 at 08:05