Passing ArrayList<Object> back to main Activity (Android)

Danny

I have an ArrayList< Object> where the Object is an instance of a class I made. I want to pass this new array list back to the main activity to put it in a navigationdrawer item.

How do I pass an ArrayList< Object> from an activity back to the main one?

I have tried the bundle.getParcelableArrayList however it tells me that "Inferred type Object for type parameter T is not within its bounds, should implement android.os.parcelable"

Here is my Code:

public void createPlaylist(String playlistName, ArrayList<Song> newPlaylistSongsArray) {
    Intent returnIntent = new Intent();
    Bundle b = new Bundle();
    b.putParcelableArrayList("array", (ArrayList<? extends Parcelable>) newPlaylistSongsArray);
    returnIntent.putExtra("playListName", playlistName);
    returnIntent.putExtra("array", newPlaylistSongsArray);
    setResult(Activity.RESULT_OK,returnIntent);
    finish();
}

And:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK){
            String newPlaylistName = data.getStringExtra("playListName");
            Bundle b = getIntent().getExtras();
            final ArrayList<Song> newPlaylist = b.getParcelableArrayList("array");
            Toast.makeText(MP3Player.this, "New Playlist Created", Toast.LENGTH_LONG).show();
        }
        if (resultCode == Activity.RESULT_CANCELED) {
            Toast.makeText(MP3Player.this, "Shit went wrong yo!", Toast.LENGTH_LONG).show();
        }
    }
}
PLe

For the easiest way, your Song class need implement Serializable.

Bundle bundle = new Bundle();
bundle.putSerializable("newPlaylist", newPlaylist);
intent.putExtras(bundle);

in other activity:

Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();

ArrayList<Song> newPlaylist=
               (ArrayList<Song>)bundle.getSerializable("newPlaylist");

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Why we need to serializable object for passing one activity to another activity in Android

Android: ClassNotFoundException when passing serializable object to Activity

sending an arraylist back to the parent activity

Android passing ArrayList<Model> to Fragment from Activity

how back to the main activity in android?

Passing Object from Fragment to Activity

Passing database class object from main activity to another activity

Passing String from Activity back to running Fragment in Android

Moving Data from ArrayList in Second activity to main activity - android

Android: Phone Back Button on Main Activity

Android: an activity with no ability to back to it

Passing ArrayList from Fragment class to Activity

ArrayIndexOutOfBoundsException / passing arraylist from alertdialog to activity

Passing the object back in a block

Android : How to keep running main activity even if the back button is pressed on the main activity screen

Passing an ArrayList of other Arrays to an Activity

android: Go Back To Main Activity Without Losing It's Data

Converting Shared Preferences saved ArrayList JSON String back to usable object throws null pointer for Activity?

Android Java - Passing data between intents but need to save it on main activity

Passing the selected object to an Another Activity

Passing Arrays back to main body

Passing ArrayList<Person> to new activity

Class to Activity passing ArrayList values

Passing an object from the 2nd activity back to main activity using serializable in android

Trouble passing new object back to main form - C# .NET

Android - What is the best practice for passing back information from an activity without onActivityResult()?

How to back to Main activity from the notification activity?

Android Studio project doesn't generate binding object for the Main Activity

Can't get back to main activity in Android studio