How can I pass several serializable using bundle intent?

I want to transfer the data between activities.

I have got these ArrayLists:

val myDataSetStrings = ArrayList<ArrayList<TrainerObject>>()
 val failTypeList = ArrayList<FailType>()
 val checkedElementsAll = ArrayList<ArrayList<Boolean>>()
 val maxRows = ArrayList<Int>()

I pack them using this code:

    var intentWithResult = Intent()
    val bundle = Bundle() 
    if (myDataSetStrings != null) {
bundle.putSerializable(Const.XMLPARSERINTENTDATA_MYDATASETSTRINGS,
                                        BundleContainer(myDataSetStrings))
                            }
    if (failTypeList != null) {
bundle.putParcelableArrayList(Const.XMLPARSERINTENTDATA_FAILTYPELIST,
                                        failTypeList)
                            }
    if (checkedElementsAll != null) {
bundle.putSerializable(Const.XMLPARSERINTENTDATA_CHECKEDELEMENTS,
                                        BundleContainer(checkedElementsAll))
                            }
    if (maxRows != null) {
bundle.putIntegerArrayList(Const.XMLPARSERINTENTDATA_MAXROWS,
                                        maxRows)
                            }
    intentWithResult.putExtras(bundle)
    setResult(Activity.RESULT_OK, intentWithResult)
    finish()

I try to retrieve the data in another activity using this code:

 private fun loadDataFromPreferences(data: Intent) {
        var activated = false
        val bundle = data.extras
        if (bundle.containsKey(Const.XMLPARSERINTENTDATA_MYDATASETSTRINGS)) {
            myDataSetStrings.clear()
            val tempMyDataSetStrings = bundle.getSerializable(Const.XMLPARSERINTENTDATA_MYDATASETSTRINGS)
                    as BundleContainer<TrainerObject>
            myDataSetStrings.addAll(tempMyDataSetStrings!!.objects)
            activated = true
        }
        if (bundle.containsKey(Const.XMLPARSERINTENTDATA_FAILTYPELIST)) {
            failTypeList.clear()
            val tempFailTypeList = bundle.getParcelableArrayList<FailType>(Const.XMLPARSERINTENTDATA_FAILTYPELIST)
            failTypeList.addAll(tempFailTypeList)
            activated = true
        }
        if (bundle.containsKey(Const.XMLPARSERINTENTDATA_CHECKEDELEMENTS)) {
            checkedElementsAll.clear()
            val tempCheckedElementsAll = bundle.getSerializable(Const.XMLPARSERINTENTDATA_CHECKEDELEMENTS)
                    as BundleContainer<Boolean>
            checkedElementsAll.addAll(tempCheckedElementsAll!!.objects)
            activated = true
        }
        if (bundle.containsKey(Const.XMLPARSERINTENTDATA_MAXROWS)) {
            maxRows.clear()
            val tempMaxRows = bundle.getIntegerArrayList(Const.XMLPARSERINTENTDATA_MAXROWS)
            maxRows.addAll(tempMaxRows)
            activated = true
        }
        if (activated) {
            reloadAllAdapters()
        }
    }

But the app fails at line:

val tempMyDataSetStrings = bundle.getSerializable(Const.XMLPARSERINTENTDATA_MYDATASETSTRINGS)
                    as BundleContainer<TrainerObject>

AndroidStudio says: this is not possible to cast ArrayList into BundleContainer.

So, I've checked the other items and found that all of them containt the same last maxRows ArrayList data. So, the keys doesn't matter. All of

is it normal? Is the only solution to use a Wrapper object that holds all of transfered items?

1 answer

  • answered 2018-01-14 00:23 AdricoM

    AndroidStudio says: this is not possible to cast ArrayList into BundleContainer.

    You serialize an ArrayList<ArrayList<YourObject>>, why don't you just deserialize it to the same type?