Ktan Help Binding Your Extras and Arguments More Easier
Do You Know Ketan (Sticky Rice)?
Ketan (sticky rice) is one of popular food / ingredient in Indonesia. Ketan (sticky rice) can be eaten directly, or with the other ingredient. Like image above, ketan (sticky rice) filled with chicken slices, so that the ketan (sticky rice) will keep and hold the chicken slices from falling apart.
Imagine like ketan (sticky rice) filled with chicken slices, we have class filled with extras. So that class will keep and hold extras, and so it will be easier to us to know what extras inside the class.
Background
Prior, I already shared the article about HappyRouter in here.
TLDR;
HappyRouter works with annotation and this annotation will tell the processor to create a Router class and an ExtrasBinding class.
But unfortunately, this HappyRouter not works well when we decided to migrate our code from Java to Kotlin. And also we face some performance issues when build our project.
And after do some researches, I interested with how view binding works. In view binding, will auto generated class and linking with our xml. So we no need to worry how we get our views, and also we have clear visibility to know our views.
Define Your Extras
Base on view binding features, we try to have similar approach. The first thing, we need to define our Extras
for particular Activity
or Fragment
And from that Extras
class, we can see all extras
that we can get for particular Activity
or Fragment
. For example, we will use this Extras
in ExampleActivity
As you can see, you will have ExampleExtrasBinding
that autogenerated from the processor, and can use it into your Activity
or Fragment
. And you no need to worry how you can get some extras
that pass into your Activity
or Fragment
, and what extras
do you have for your Activity
or Fragment
.
At this point, maybe you are have another approach to achieve this. You can use lazy delegate, or create some delegation to setter getter the extras
or arguments
. But when you want to open ExampleActivity
, how do you know what extras need to pass to open ExampleActivity
properly?
And you need to create static function for each your Activity over and over again. And also you need to maintain key each extras
in multiple places (in static function, and in your Activity
or Fragment
. So, Ktan here to help to achieve this.
Define Your Route
After you define your Extras
, to make easier for routing to your Activity
or Fragment
, you need to define Route
in your Activity
or Fragment
. It can be achieve with annotate your Activity
or Fragment
with Route
As you can see, Ktan will generate extension function routeToExampleActivity
and you will know what extras / data need to pass to open ExampleActivity
properly.