Apostrofix
Apostrofix

Reputation: 2180

How to avoid a circular reference between projects?

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?

Upvotes: 3

Views: 2897

Answers (1)

daentech
daentech

Reputation: 1115

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.

Upvotes: 2

Related Questions