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:
- Creating Bundle() and passing that bundle using putExtra()
- 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:
- Created a singleton object to store the ArrayLists
object ZipEntrySingleton {
var aList: ArrayList<ZipEntry>? = null
var lList: ArrayList<ZipEntry>? = null
}
- 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)
- Retrieved the data in the new activity by using:
ZipEntrySingleton.aList