Android Send Object to Another Activity Through Intent

July 22, 2018

The object must be either Serializable or Parcelable.

For example, Location class is Parcelable, so it can be used to be passed between activity.

val EXTRA_LOCATION = "location"

val intent = Intent(context, PlacePickerActivity::class.java).apply {
    putExtra(EXTRA_LOCATION, location)
}

startActivity(intent)

To receive Parcelable from Intent.

val location = intent.getParcelableExtra(EXTRA_LOCATION)
val location = intent.extras.get(EXTRA_LOCATION) as Location?

For your own class, the easiet way is to implement Serializable.

class PlaceAutoComplete(val id: String, val name: String) : Serializable {

}
val place = intent.getSerializableExtra(EXTRA_PLACE) as PlaceAutoComplete?

References:

This work is licensed under a
Creative Commons Attribution-NonCommercial 4.0 International License.