Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NavigationDrawer with Activities vs. NavigationDrawer with Fragments

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?

like image 617
Ionut Ciuta Avatar asked Sep 07 '15 09:09

Ionut Ciuta


People also ask

What is navigation drawer activity?

The navigation drawer is a UI panel that shows your app's main navigation menu. The drawer appears when the user touches the drawer icon in the app bar or when the user swipes a finger from the left edge of the screen.

What are fragments and activity?

Activity is an application component that gives a user interface where the user can interact. The fragment is only part of an activity, it basically contributes its UI to that activity. Activity is not dependent on fragment. Fragment is dependent on activity. It can't exist independently.


1 Answers

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.

like image 196
Rinav Avatar answered Oct 03 '22 18:10

Rinav