Collectives™ on Stack Overflow
Find centralized, trusted content and collaborate around the technologies you use most.
Learn more about Collectives
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
Learn more about Teams
if(edtNombre.text.toString().equals("") ||
edtApellido.text.toString().equals("") ||
edtFecha.text.toString().equals("")){
Toast.makeText(this@Main2Activity, "Debes llenar todos los campos!", Toast.LENGTH_SHORT).show()
}else{
var estadoSel : String
estadoSel = estado.onItemSelectedListener.toString()
var per = Persona(edtNombre.text.toString(),edtApellido.text.toString(),estadoSel,edtFecha.text.toString())
personas.add(per)
val intent = Intent(this@Main2Activity,Main3Activity::class.java)
//intent.putExtra("Personas", personas as Serializable)
intent.putParcelableArrayListExtra("Personas", ArrayList(personas))
edtFecha.text = null
edtApellido.text = null
edtNombre.text = null
estado.setSelection(0)
Toast.makeText(this@Main2Activity, "Registro Guardado", Toast.LENGTH_SHORT).show()
Activity B:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main3)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
//val intent = Intent()
val bundle = getIntent().extras
var arr = bundle?.getParcelableArrayList<Persona>("Personas")!!
//var arr : ArrayList<Persona> = (ArrayList<Persona>())intent.getSerializableExtra("Personas")
//intent.getSerializableExtra("Personas")
//arr = intent.extras!!.get("Personas")
val adapter = ArrayAdapter(this, android.R.layout.simple_list_item_1, arr)
listaPersonas.adapter = adapter
But I'm getting a Null Pointer Exception
–
In your app's build.gradle under android scope add this and sync gradle -
androidExtensions {
experimental = true
Then make your object class parceble as follows -
@Parcelize
data class Persona() : Parcelable {
Now you can send and receive Parcelable list by intent.
Update : @Parcelize is no more experimental now, follow this answer for updated implementation https://stackoverflow.com/a/64925204/9854554
To pass List of Data via intent implement Parcelable
interface on Model class.
Person Model Class:
import android.os.Parcel
import android.os.Parcelable
data class Person(val name: String?, val age: Int) : Parcelable {
constructor(source: Parcel) : this(
source.readString(),
source.readInt()
override fun describeContents() = 0
override fun writeToParcel(dest: Parcel, flags: Int) = with(dest) {
writeString(name)
writeInt(age)
companion object {
@JvmField
val CREATOR: Parcelable.Creator<Person> = object : Parcelable.Creator<Person> {
override fun createFromParcel(source: Parcel): Person = Person(source)
override fun newArray(size: Int): Array<Person?> = arrayOfNulls(size)
MainActivity:
class MainActivity : AppCompatActivity() {
var personList = arrayListOf<Person>()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
personList.add(Person("Person1", 31))
personList.add(Person("Person2", 32))
personList.add(Person("Person3", 33))
var intent = Intent(this, BActivity::class.java)
intent.putParcelableArrayListExtra("personList", personList)
Handler().postDelayed( {startActivity(intent)}, 2000)
BActivity:
class BActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_b)
val extras = intent.extras
val personList = extras?.getParcelableArrayList<Person>("personList")
if (personList != null) {
personList.forEach { Log.i("BActivity", "Name: ${it.name} Age: ${it.age}" ) }
}else{
Log.i("BActivity", "PersonList in null");
In your activity B to get your list try like the following
var arr = this.getIntent().getParcelableArrayListExtra<Parcelable>("Personas")
and make sure your Persona
is extend Parcelable
like below.
class Persona() : Parcelable {
// ....
@Override
public void writeToParcel(Parcel dest, int flags) {
//....
private void readFromParcel(Parcel in) {
//....
override fun describeContents(): Int {
return 0
companion object CREATOR : Creator<Persona> {
override fun createFromParcel(parcel: Parcel): Persona{
return Persona(parcel)
override fun newArray(size: Int): Array<Persona?> {
return arrayOfNulls(size)
In your, Activity A send array list with
intent.putParcelableArrayListExtra("Personas", personas)
In your, Activity B get array list with
intent.getParcelableArrayListExtra("Personas")
In your app's build.gradle inside android tag add this and sync with gradle -
androidExtensions {
experimental = true
Data Class
@Parcelize data class Persona(
val name: String,
) : Parcelable
val intent = Intent(this, B::class.java)
intent.putParcelableArrayListExtra("list", personaArrayList)
startActivity(intent)
Step 4: In activity B for recieving data:
val list = intent.getParcelableArrayListExtra<Persona>("list")
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.