15

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?

Ionut Ciuta
  • 618
  • 1
  • 9
  • 24
  • why do you try to invent a bicycle - there is a described way how to do that and tons of examples. Nowadays you have NavigationView that gives you material design out of box. Your main activity should manage navigation and state. Each fragment should give you logic separation. If you have more than 100 lines of code in your activity - that could mean that you do something wrong. – Viktor Yakunin Sep 07 '15 at 09:41
  • Thanks for answering. I've just started looking into NavigationView and I'll probably take care of the main activity thing too. Thanks again. – Ionut Ciuta Sep 07 '15 at 09:55

3 Answers3

10

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.

Community
  • 1
  • 1
Rinav
  • 2,527
  • 8
  • 33
  • 55
  • Thank you for your answer. I ended up doing the same since it was really hard for me to manage in a single activity fragments which had completely different purposes. I ended up with a BaseActivity like you've mentioned and a specialized activity extending the previous for each group of fragments (list fragments, edit fragments, view). fragments) – Ionut Ciuta Oct 05 '15 at 11:37
  • I am glad that you font your solution, you may accept this as your answer ;) – Rinav Oct 05 '15 at 11:51
7

I have the following points to submit:

  1. The fragment approach is much better. You should use fragments for better UI experience for the user.

  2. Think like this, imagine your screen as a basket of information and if you have another basket(i,e another screen) with which a lot of data will have to be transferred to and fro, then, according to me, it is much better to use fragments for the two baskets along with a container activity. And of course there can be more than two baskets/screens.

  3. There is no hard and fast rule that you should use only use fragments or activities, but google says that it is much better to use fragments where ever it is possible.

  4. Generally, developers use fragments to group associated logic together and it is much better to do it this way as it will provide a logical grouping of whatever you are trying to do.

  5. It is also easy to pass java data objects among fragments via the container activity and with the help of Interfaces. This is also considered a very modular approach.

Rest depends on how you would like to define the flow of your application. I think using fragments is a much better approach in your scenario. Use container activities wherever you think that the associated logic has changed drastically.

Kaveesh Kanwal
  • 1,753
  • 17
  • 16
  • I really appreciate your help. I'm going to keep the fragment approach and revise the code I've written till now for better communication between fragments. Thanks a lot! – Ionut Ciuta Sep 07 '15 at 21:10
2

First off, managing navigation between fragments is not difficult. You can check google's tutorial. I can suggest you some edits if you post your code

I think it is bad design for two reasons

  • Lot of code rework
  • If you want to change your navigation pattern to say tabs, it can't be done easily.
Nick
  • 949
  • 1
  • 11
  • 31