Pass objects between Android activities

Jimmy leo
1 min readJul 16, 2021

--

Photo by Vijay Sekhar Sadineni on Unsplash

If you are an android developer, you must know passing object between objects is painful sometimes, it requires some boilerplate code.

Normally, we implement Parceble interface for our model class, manually write every fields into Parcel object. And use these two method to put and get your object.

intent.putExtra("xx", yourObject)
intent.extras.getParcelable("xx")

To avoid implement Parceble interface, i created this library (intentparser) to simplify the process.

First, add dependency.

Add this to your project gradle
```
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```Add this to your application gradle
```
dependencies {
implementation 'com.github.lau1944:intentparser:v$currentVersion'
}
```

To put object into bundle

Current Acitivity
val testModel = TestModel(
text = "hello world",
isSuccess = false,
testNum = 1,
textModelSec = TextModelSec("second model")
)
startActivity(
Intent(this, ActivityTest::class.java).apply {
this.putObject(testModel)
}
)

To get object

val testModel = intent.getObject(TestModel::class.java)

That is ! I use kotlin extension to simplify everything.

  • Please note that under the hood, i use putStringExtra, so do not put any big object into the method, or it would cause data leak.

https://github.com/lau1944/intentparser

--

--

Jimmy leo
Jimmy leo

No responses yet