Passing and retrieving MutableList or ArrayList from Activity A to B

Eduard Unruh

I need to pass this:

private lateinit var memes: MutableList<Memes>

which has this model:

class Memes (
    @SerializedName("id") var id: Long,
    @SerializedName("title") var title: String
)

from activity a to b.

I've altready seen couple "solutions" and none of them works!

This is the last that I've tried:

    val extras = Bundle()
    val memesArrayList = ArrayList(memes)
    val i = Intent(context, GalleryShow::class.java)
    i.putExtras(extras)
    i.putStringArrayListExtra("list", memesArrayList)
    (context as Activity).startActivityForResult(i, 777)

However, I get Type mismatch: inferred type is ArrayList<Memes!> but ArrayList<String!>? was expected on memesArrayList.

EDIT:

This is my latest attempt now:

In activity A inside recyclerview item:

val extras = Bundle()

extras.putString("gal", "animals")
extras.putString("query", "")

val i = Intent(context, GalleryShow::class.java)
i.putExtra("list", memes.toTypedArray())
i.putExtras(extras)
(context as Activity).startActivityForResult(i, 777)

and this is inside activity B:

private lateinit var memes: MutableList<Memes>


override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_gallery_show)

    memes = (this?.intent?.extras?.get("list") as? Array<Memes>)?.asList()!!.toMutableList()
}
Tenfour04

You can use simply intent.putExtra instead of worrying about which variant like put_____Extra to use.

When extracting the value, you can use intent.extras to get the Bundle and then you can use get() on the Bundle and cast to the appropriate type. This is easier than trying to figure out which intent.get____Extra function to use to extract it, since you will have to cast it anyway.

The below code works whether your data class is Serializeable or Parcelable. You don't need to use arrays, because ArrayLists themselves are Serializeable, but you do need to convert from MutableList to ArrayList.

// Packing and sending the data:
val i = Intent(context, GalleryShow::class.java)
i.putExtra("list", ArrayList(memes)) // where memes is your MutableList<Meme> property
startActivityForResult(i, 777)

// Unpacking the data in the other activity:
memes = intent.extras?.get("list") as MutableList<Meme>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

passing string from an activity to a fragment

Retrieving a random item from ArrayList

Retrieving data from Firebase in Android activity

Android passing ArrayList<Model> to Fragment from Activity

Passing Object from Fragment to Activity

Pass a mutableList from activity A to activity B in Kotlin.

Passing a variable from an activity to a class

Passing Parameters and Retrieving Return Results from thread

Passing Value from Activity to Fragment

Passing values from Activity to Fragment

Passing data from activty B to activity A fragment

Passing data to activity from widget

Passing ArrayList from Fragment class to Activity

Passing variable from Activity to Adapter

ArrayIndexOutOfBoundsException / passing arraylist from alertdialog to activity

Trouble retrieving and passing data from alertdialog to listview

Passing an ArrayList of other Arrays to an Activity

Retrieving ArrayList<Object> from FireBase inner class

Passing Parameters from Activity to Another Activity Fragements

Unchecked Cast error while passing an arraylist from one activity to another using intent

Passing data from second activity to first activity

Pass ArrayList from to Activity

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

Why am I getting a null pointer exception when passing an ArrayList of strings from one activity to another?

Passing ArrayList<Person> to new activity

Class to Activity passing ArrayList values

Passing Interface To Fragment From Activity

Pop Mutablelist from a MutableList kotlin

Retrieving values from an ArrayList of IntArrays