Home > Software engineering >  Pass ArrayList<ZipEntry> from one activity to another
Pass ArrayList<ZipEntry> from one activity to another

Time:01-11

So for one of my personal projects I want to pass an arraylist of ZipEntry objects from one activity to another but I am unable to do so. I have tried the following things:

  1. Creating Bundle() and passing that bundle using putExtra()
  2. Passing ArrayList directly using putExta()

Creating bundle & passing it using putExtra(): Implementation:

// Add data to intent and launch install activity
val newActIntent = Intent(this, InstallActivity::class.java)
val data = Bundle()
data.putSerializable("x", languageListAdapter.selectedItems)
newActIntent.putExtra("z", data)
this.startActivity(newActIntent)

Error:

java.lang.IllegalArgumentException: Parcel: unknown type for value split_config.en.apk

Passing ArrayList<> directly using putExtra() Implementation:

val newActIntent = Intent(this, InstallActivity::class.java)
newActIntent.putExtra("x", languageListAdapter.selectedItems)
this.startActivity(newActIntent)

Error:

java.lang.IllegalArgumentException: Parcel: unknown type for value split_config.en.apk

Note: ZipEntry object is java.util.zip.ZipEntry

CodePudding user response:

In order to send an ArrayList<ZipEntry> from one Activity to another you need to make sure that your ZipEntry object is serializable, you can do it by implementing Serializable interface. You can also use Parcelable as an alternative of Serializable.

If you use Serializable you use getSerializableExtra and if it's Parcelable you use getParcelableArrayListExtra

@Parcelize
data class ZipEntry : Parcelable

CodePudding user response:

Fixed this issue thanks to the comment from @wambada

Implementation:

  1. Created a singleton object to store the ArrayLists
object ZipEntrySingleton {
    var aList: ArrayList<ZipEntry>? = null
    var lList: ArrayList<ZipEntry>? = null
}
  1. Created the singleton object in activity 1 and passed the data to it.
// Add data to intent and launch install activity
val newActIntent = Intent(this, InstallActivity::class.java)
val x = ZipEntrySingleton
x.aList = xListAdapter.selectedItems
x.lList = yListAdapter.selectedItems
this.startActivity(newActIntent)
  1. Retrieved the data in the new activity by using:
ZipEntrySingleton.aList
  • Related