NavigationDrawer with Activities vs. NavigationDrawer with Fragments

Ionuț Ciuta picture Ionuț Ciuta · Sep 7, 2015 · Viewed 8.3k times · Source

Take for example the app I'm currently working on: - it has an navigationDrawer with multiple items; there are two items which interest me for now, i'll call them X and Y.

  • both X and Y, when clicked on, display a fragment containing a list of x-elements or y-elements

  • selecting and x or y list element displays a new fragment in which I display info about the select item; the view fragments are different for x and y elements

  • in the view fragment I can choose to edit the specific element which brings up an edit fragment

The fragment approach is working, but it took me a while to manage the navigation between the fragments. Also, I will probably have to add some new items in the drawer similar with X and Y. My main activity, in which I have the drawer and I do the fragment switching, is quite dense already, which brings me to my question: should I switch from fragments to activities? I was thinking about starting a new activity when a drawer item is selected and handle the list/view/edit fragments related to the selected item in that activity, rather than handling all fragments for all items in a single activity.

Is it a good idea? Is it bad design?

Answer

Rinav picture Rinav · Oct 5, 2015

I was in similar boat and I used Activities method, that way I have each Activity with group of fragments for a particular nav click on the NavigationView. Obviously I use NavigationView, but managing all those fragments with just one MainActivity is really a painful task.

I rather prefer EachActivity managing their own fragments when we click a nav item. This gives me a better performance, since I do not need to worry about lifecycle of soo many fragments and it's backStack and add/remove/show/hide hell.

I used the following SO Question to cleverly use a BaseActivity implementing NavigationDrawer, and sharing it with all the other Activities. The real magic is it does not duplicate code, or it's not just plain old Inheritance technique.

Here's the link, do check it out

I used this method in two of my project's and it's running very well plus I do not have to deal with fragment management right from the very start.