0

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?

Filnor
  • 1,290
  • 2
  • 23
  • 28
tore
  • 619
  • 2
  • 9
  • 23
  • 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 03 '18 at 06:42
  • 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 03 '18 at 06:51
  • I will try to suggest an answer. Please confirm it works :) – tompee Jul 03 '18 at 06:54

1 Answers1

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.

tompee
  • 1,408
  • 1
  • 7
  • 6
  • Did you just add `: Parcelable` ? I tried, but it says that `Expecting a top level declaration` – tore Jul 03 '18 at 07:17
  • 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 03 '18 at 07: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 03 '18 at 07: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 03 '18 at 07:33
  • Note that @Parcelize is an experimental feature. The implementation and specification can change in the future so beware. – tompee Jul 03 '18 at 07:35