How do I receive parcelable bundles?
How do I receive parcelable bundles?
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.
bundle
The class object stored in the array RecipeTemplate
is Parcelized
and looks like this:
RecipeTemplate
Parcelized
@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?
recipeArray
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?
Property not serialized as parcel
Oh, that's just me trying different object variables. I'm sure its not null. I'll update the question. Thanks for pointing it out :)
– tore
Jul 3 at 6:51
I will try to suggest an answer. Please confirm it works :)
– tompee
Jul 3 at 6:54
1 Answer
1
Try refactoring your RecipeTemplate to accept the properties as parameters in your constructor instead.
@SuppressLint("ParcelCreator")
@Parcelize
class RecipeTemplate (
var recipeHeader: String? = null,
var recipeText: String? = null,
var recipeImage: String? = null,
var recipeKey: String? = null
) : Parcelable
The problem may lie on the way the parcelize is implemented. I cannot find any documentation regarding this but there is a high chance that createFromParcel just calls the primary constructor only. This is still experimental and may change in the future. I can be wrong though and I am glad to be corrected.
Did you just add
: Parcelable
? I tried, but it says that Expecting a top level declaration
– tore
Jul 3 at 7:17
: Parcelable
Expecting a top level declaration
I moved all the properties inside the parentheses (yours was curly braces) and moved the interface at the end. Added commas as well since they are already parameters
– tompee
Jul 3 at 7:19
Dude, it worked! Awesome man. Thanks a bunch for taking the time to help me out! :D Do you think you could elaborate a bit on what I just did?
– tore
Jul 3 at 7:28
This is related to how Parcelize is implemented by Kotlin. In your previous class definition, everything is declared as properties. Therefore, when you instantiate your class using the default constructor, you have to initialize your properties manually. I think Parcelize calls the primary constructor only, therefore, it can instantiate your class but it cannot set the property values. That's why everything is null. To solve this, you have to set your properties as primary constructor parameters so parcelize can set their values.
– tompee
Jul 3 at 7:33
Note that @Parcelize is an experimental feature. The implementation and specification can change in the future so beware.
– tompee
Jul 3 at 7:35
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Are you sure your recipe image is not null? Because on the first activity, you are checking the recipe header. On the 2nd one, you are checking the image.
– tompee
Jul 3 at 6:42