Simplifying Android development with HappyRouter

Ade Fruandta
HappyTech
Published in
2 min readOct 29, 2018
“white and black One Way-printed road signages” by Brendan Church on Unsplash

Recently, Google announced a new component for Android development, called Navigation Architecture Component. I think it’s similar with the Controller in iOS. With this component, you can map each screen with certain extras according to what are needed in that screen. The safe args plugin will help you to navigate to another screen.

However, in our project at HappyFresh, there is something unfit if we implement this component. So, we decided to create our router, called HappyRouter.

The idea of HappyRouter is similar to Navigation Architecture Component, but is different in terms of implementation. HappyRouter works with annotation and this annotation will tell the processor to create a Router class and an ExtrasBinding class.

How it works

Router class will be generated for every activity/fragment class annotated with @Route. This class will help us to start activity or create a fragment. @Route annotation only is not helpful enough to us for develop Android app. @Extra annotation help us more in developing Android app.

@Extra annotation helps Router class to define what data is needed to open an activity/fragment. See an example below:

@Route
public class ProductDetailActivity extends AppCompatActivity {

@Extra(key = "title", required = true)
String title;
@Extra(key = "subtitle", required = false)
String subtitle;
}

In this class, the processor is told to create a ProductDetailActivityRouter class & ProductDetailActivity_ExtrasBinding class.

ProductDetailActivityRouter

public class ProductDetailActivityRouter extends BaseRouter {   public ProductDetailActivityRouter(String title) {      intent.putExtra("title", title);   }   public void putSubtitle(String subtitle) {      intent.putExtra("subtitle", subtitle);   }   public Intent create(Context context) {      return super.create(context, ProductDetailActivity.class);   }}

ProductDetailActivity_ExtrasBinding

public class ProductDetailActivity_ExtrasBinding extends ExtrasBinding {   public ProductDetailActivity_ExtrasBinding(ProductDetailActivity target, Bundle bundle, Bundle... optionals) {

target.title = (String) get("title", target.title, String.class);
target.subtitle = (String) get("subtitle", target.subtitle, String.class);
}}

After the two classes had been generated, you can use it like the example below:

@Route
public class ProductDetailActivity extends AppCompatActivity {

@Extra(key = "title", required = true)
String title;
@Extra(key = "subtitle", required = false)
String subtitle;
protected onCreate(Bundle savedInstanceState) { // For bind each extras into title & subtitle
Router.bind(this, getIntent().getExtras(), savedInstanceState);
}}

If some screens need to open ProductDetailActivity , you can just call information from ProductDetailActivityRouter as shown below:

@Route
public class HomeActivity extends AppCompatActivity {
public void openProductDetail(String title, String subtitle) { ProductDetailActivityRouter router = new ProductDetailActivityRouter(title);
router.putSubtitle(subtitle);
Intent intent = router.create(this);
startActivity(intent);
}}

Maybe it’s not perfect, but hopefully it can support your Android development.

Clap if you like this post.

For more information, see the link below:

--

--