Android Send Object to Another Activity Through Intent

Jul 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:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.