Ktan With More Extras? Why Not!

Ade Fruandta
2 min readSep 5, 2022

--

Photo by Max Griss on Unsplash

Imagine we have Ketan (sticky rice) with chicken and mayonnaise, or chicken with sprinkled sesame seeds like image above. Looks tasty right?

Same in this article, we will talk about how to use Ktan with multiple extras.

Background

Prior this article, I already explain about how to use Ktan in your project and will help you to improve your code in here. But as I mention before, that only a few feature I explain.

Imagine we have case where our Activity or Fragment extends from another Activity or Fragment . Or our Activity or Fragment need extras that already define in another package. Ktan is here to solve / fix this case.

How To

In example we have FirstActivity and FirstExtras , and also SecondActivity where extends to FirstActivity and have SecondExtras

@Route(
extras = [FirstExtras::class]
)
open class FirstActivity : AppCompatActivity() {
val firstExtrasBinding: FirstExtrasBinding by bindExtras()
}
@Route(
extras = [SecondExtras::class]
)
class SecondActivity : FirstActivity() {
val secondExtrasBinding: SecondExtrasBinding by bindExtras()
}

Like you see, if we want to open SecondActivity with routeToOpenSecondActivity , we will provideSecondExtras only. So we will skip pass FirstExtras that required by FirstActivity , and our code will break. So what should we do?

We need to add FirstExtras to Route at SecondActivity

@Route(
extras = [SecondExtras::class, FirstExtras::class]
)
class SecondActivity : FirstActivity() {
val secondExtrasBinding: SecondExtrasBinding by bindExtras()
}
// in another function to open SecondActivity
fun openSecondActivity() {
routeToOpenSecondActivity({
// this block for SecondExtras
},{
// this block for FirstExtras
})}

With this, hopefully every time and anyone want to open SecondActivity , they will know what extras need to pass to open SecondActivity properly.

Another Case

Let say, we have SecondActivity like this. We extends with AppCompatActivity but we need all extras that already define for FirstActivity .

@Route(
extras = [SecondExtras::class, FirstExtras::class]
)
class SecondActivity : AppCompatActivity() {
val firstExtrasBinding: FirstExtrasBinding by bindExtras()
val secondExtrasBinding: SecondExtrasBinding by bindExtras()
}
// in another function to open SecondActivity
fun openSecondActivity() {
routeToOpenSecondActivity({
// this block for SecondExtras
},{
// this block for FirstExtras
})}

This case also can solve by Ktan. So, to minimize duplication code, you can reuse FirstExtras that already define before to use in SecondActivity .

Thanks for reading. I hope you enjoy to read this short article. And more interested to use Ktan. And don’t forget to visit Ktan Github.

👏 if you like it, and more 👏 👏 👏 if you really really like it.

--

--