I create a bundle like this:
val intent = Intent(classContext, Recipes::class.java)
var bundle = Bundle().apply {
putParcelableArrayList("LIST", ArrayList<Parcelable>(fbModel.recipeArray))
putInt("POSITION", position)
}
intent.putExtra("bundle", bundle)
//CHECK TO SEE IF DATA IS STORED
var passedIntent = intent.extras
var bundle2: Bundle = passedIntent.getBundle("bundle")
var recipeArray: ArrayList<RecipeTemplate> = bundle2.getParcelableArrayList("LIST")
Log.d("TAGC", " " + recipeArray[0].recipeHeader) //SUCCESS!
Log.d("TAGC", " " + position) //SUCCESS!
startActivity(intent)
To see if it worked I created and logged variables from the bundle
and they do indeed contain the correct data.
The class object stored in the array RecipeTemplate
is Parcelized
and looks like this:
@SuppressLint("ParcelCreator")
@Parcelize
class RecipeTemplate: Parcelable {
var recipeHeader: String? = null
var recipeText: String? = null
var recipeImage: String? = null
var recipeKey: String? = null
}
So far so good. But when I receive the bundle in the other activity it returns null for some reason, even though I use the same exact code as above (the test code to see if the bundle stored the correct data). This is the receiving activity:
var passedIntent: Bundle = intent.extras
var bundle = passedIntent.getBundle("bundle")
var counter: Int = bundle.getInt("POSITION", 0)
var recipeArray: ArrayList<RecipeTemplate> = bundle.getParcelableArrayList("LIST")
Log.d("TAGA", "PASSED " + counter) //SUCCESS
Log.d("TAGA", "PASSED " + recipeArray[0].recipeHeader) //FAIL: null
The counter/position variable returns the correct data, but the recipeArray
is null for some reason. Again, it worked in the previous activity, so I don't see why it's different this time around... Any ideas?
UPDATE
If I hover the cursor over the variables in the class it says: Property not serialized as parcel
. Sounds like things aren't working as I intended... What gives?