NavigationDrawer activity cluttered with fragment callbacks and application business logic

An Activity containing a NavigationDrawer must juggle it with fragments. My problem is that the box should be accessible on all possible screens of the application, which makes my only MainActivity activity very cluttered with fragment-callback code and various types of navigation / business logic.

As the application grows, it becomes more difficult to navigate this activity, and I began to think about possible alternative approaches. The new approach should maintain the same visual behavior of the original and eliminate clutter.

In addition to the navigation box events, there are several fragments that also contain navigation / business logic, which must also be processed by MainActivity. For example, a fragment may contain 3 or more buttons that will launch other fragments or execute some business logic with cross-concern.

So, the resulting number of listener interfaces implemented by MainActivity is growing and currently stands at 20. Perhaps you think this does not look and does not seem good.

I think I could divide things into several NavigationDrawer actions to facilitate maintenance. This implies an increase in resource consumption and a slight deviation of visual effects, since new actions will only be launched after the box is closed, contrary to the initial approach, which instantly changes fragments.

Do you think this is a bad idea? How can this be improved? Or is there a better solution?

Thanks.

UPD clarified the description.

+7
android android-activity android-fragments navigation-drawer android-navigation
source share
2 answers

You said that you have only one action. So, I assume that all screens are fragments in your application. Because of this, NavDrawer will be available at any time in your application by default.

There is no need for multiple actions with various NavDrawer implementations. You can use one BaseActivity to handle the implementation of NavDrawer and with inheritance, which you can use in every active action, if you want to implement more in the future. This will follow OOP principles and lead to cleaner code. In addition, NavDrawer will look and behave the same in all actions. To do this, there is one navigation menu for your application.

The objective of the Activity, which extends BaseActivity, is to process Fragment transactions and communicate with them through callbacks.

With this navigation, your application is clearly structured and definitely a way to go.

You can follow this wonderful complete tutorial that does something like this. This is a bit overwhelming at first glance, but you can get the basic idea.

+4
source share

I would suggest that you have a NavigationDrawerDelegate class that takes care of all the navigation logic and adds it to your actions and simply delegates it. the example is beautifully made here

+3
source share

All Articles