3

I have an android application project that also has a few android library projects. Each library project has it's own activities and resources. I've implemented the NavigationDrawer and would like to have it on all activities, so I created a base project (android library project) that has the drawer (following the guides in this answer: Same Navigation Drawer in different Activities). Then the idea is that each sub project (android library projects) will extend from the base but at this point I am hitting the circular reference problem.

I have a similar structure as on this image:

flow_diagram

There's no problem to have each activity in the Sub projects to extend from the Base library, but the problem is that the Base library also needs to know the activities(hence the circular reference), so that I can handle the clicks in the NavigationDrawer, something similar to that:

mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        switch (position) {
            case 0:
                Intent intent = new Intent(this, SubProjectOneActivityOne.class);
                startActivity(intent);
                finish();
                break;
            case 1:
                Intent intent1 = new Intent(this, SubProjectOneActivityTwo.class);
                startActivity(intent1);
                break;
            case 2:
                Intent intent2 = new Intent(this, SubProjectTwoActivityOne.class);
                startActivity(intent2);
                break;
            default:
                break;
    }
}});

So at the moment I am hitting a dead end with this approach. Could it be possible to overload the click event in the sub projects? Does anybody have other suggestions?

Community
  • 1
  • 1
Apostrofix
  • 2,140
  • 8
  • 44
  • 71
  • I've handled this by providing a `router` object. The base project can expose that and each of the other modules can register a route. Those routes can perform an action which may or may not be launching an activity. – daentech Dec 21 '15 at 11:27
  • @daentech Can you share more info and possibly code? – Apostrofix Dec 21 '15 at 11:31
  • Here's some sample code. I hope it compiles, but I've removed some unnecessary code. You could name your routes as constants in your base project and reference them in the other projects possibly? https://gist.github.com/daentech/f372f3a6529d08979f1c This can also handle wildcard routes using `/route/` and will pass the data in the intent as the `getData()` – daentech Dec 21 '15 at 11:38
  • @daentech What is `TransitionUtils` ? – Apostrofix Dec 21 '15 at 11:50
  • Sorry, I've removed that if you refresh it. I have some static helpers to manage animated transitions between activities – daentech Dec 21 '15 at 11:51

1 Answers1

2

I've solved this problem (and a couple of others) using a routing mechanism.

My solution is in this gist:
https://gist.github.com/daentech/f372f3a6529d08979f1c

It will allow you to add a route based on String names and launch either an activity or a callback by opening these named routes.

Your base project can share a singleton router object which the other projects can have injected. Each of these sub projects can add their routes to the router and the base project doesn't need to know anything about what these routes do, just their names.

You can also use wildcard routes, but I'm planning on updating this at some point to handle route variables and allow URLs.

The nice thing about routing with something like this is you can handle routes from anywhere in the app to anywhere else, even from something like a notification being tapped.

daentech
  • 1,125
  • 12
  • 16
  • I am not sure that I can understand how to use it. I've added the `ApplicationRouter` and the interface to the base library, but there is an error at `SyncStateContract.Constants.ACTIVITY_PATH`. And then I am not sure how to use it in the other projects. Can you please elaborate more? – Apostrofix Dec 21 '15 at 18:20
  • Are each of your other projects library modules? In Android Studio? – daentech Dec 21 '15 at 18:23
  • One is the main application and all the others are library modules, including the base library. – Apostrofix Dec 21 '15 at 18:25
  • Do each of the libraries have a reference to the base library? – daentech Dec 21 '15 at 18:26
  • Yes, but I can change the references, if they should be referenced in another way. – Apostrofix Dec 21 '15 at 18:35
  • So, the main app implements the router, or at least makes an instance. If you pass it into each of the other library modules and each of them can add routes to their activities. And then the navigation drawer can call `router.openRoute` to open each of the referenced activities. – daentech Dec 21 '15 at 18:37
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/98592/discussion-between-apostrofix-and-daentech). – Apostrofix Dec 21 '15 at 18:41