Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationDrawer activity is cluttered with fragment callbacks and application business logic

An Activity containing NavigationDrawer is supposed to juggle its fragments. My problem is that the drawer has to be available on all possible screens of the application, which makes my only activity MainActivity very cluttered with fragment-callback code and different sorts of navigation/business logic.

As the application grows it becomes harder to navigate through the activity and I've started to think about possible alternative approaches. New approach must maintain the same visual behavior of the original and remove the clutter.

Apart from navigation drawer events, there're multiple fragments that also contain navigation/business logic, which is also to be handled by the MainActivity. For instance, a fragment might contain 3 or more buttons that would launch other fragments or perform some cross-concern business logic.

So .. the resulting amount of listener interfaces implemented by MainActivity grows and as of this moment amounts to 20. You might imagine it doesn't look or feel any good.

I think I might decouple things to multiple NavigationDrawer activities to ease the maintenance. It suggests larger resource consumption and slight visual effects deviation, since new activities will be launched only after the drawer is closed contrary to the original approach which changes fragments instantly.

Do you think that it's a bad idea? How can it be improved? Or there's a better solution?

Thanks.

UPD refined the description.

like image 774
midnight Avatar asked Oct 18 '13 08:10

midnight


2 Answers

You said, you have only one Activity. So, I assume that all screens are Fragments in your App. Because of this the NavDrawer would be available at any time in your App by default.

There is no need for multiple Activitys with different implementations of the NavDrawer. You could use one BaseActivity to handle the implementation of the NavDrawer and with inheritance you can use this in every Activity you like, if you wanna implement more in the future. This would follow the OOP principles and leads into cleaner code. Furthermore the NavDrawer will look and behave the same in every Activity. Which is the purpose of it, to have one navigation menu for your App.

The job of the Activity, which extends the BaseActivity, is to handle the transactions of the Fragments and the communication with them through callbacks.

With this the navigation of your App is clear structured and is definitly the way to go.

You can follow this very nice complete tutorial which does something like this. It is a bit overwhelming on the first look, but you can get the basic idea.

like image 51
Steve Benett Avatar answered Nov 15 '22 22:11

Steve Benett


I would suggest just having a NavigationDrawerDelegate class which takes care of all the navigation logic and add it to your activities and just delegate to it. an example is nicely done here

like image 3
Mouna Cheikhna Avatar answered Nov 15 '22 22:11

Mouna Cheikhna