4

I'm in the process of building an Android app, and I have a question about the best structure. I'm using a Navigation Drawer UI, and it seems that Android pushes for you to use a fragment for each view associated with the list items in the navigation drawer.

Where that seems to get complex very quickly, is each of my fragments will have a couple of fragments (in the interest of reusability). The fragment interaction listener need to communicate with an Activity, but the only Activity I have involved is the one containing my navigation drawer. I don't want to have a lot of listeners linking back to that Activity - ideally I'd like them to link to their parent fragment.

I haven't done Android development in a while, and I want to make sure I doing this the correct way. Anyone know the best way to handle this?

coder
  • 10,460
  • 17
  • 72
  • 125
  • 1
    http://developer.android.com/training/implementing-navigation/temporal.html read a few of the best practices. Dont get hung up on design because its not a big deal to refactor activity to fragment. IMO the only sticky area getting into nav-drawer and associated fragments is handling orientation changes and the back-stack. – Robert Rowntree Mar 31 '14 at 13:42
  • I guess the answer would depend on how you're going to nest fragments inside other fragments. Sounds like something unusual to me.. And as for the good practice, I would start off making an AbstractNavDrawerActivity class, incapsulating the nav-drawer code, and extend your activity from it. That would help you to concentrate on your communication with fragments without being distracted by the heap of code belonging to navigation drawer logic. – Drew Apr 03 '14 at 19:28

1 Answers1

0

If you want nested fragments I suggest you use a wrapper fragment. This wrapper fragment will be called when an item in the navigation drawer is selected. Inside this wrapper fragment the other fragments will be shown. This way you can do everything from that item in the navigation drawer inside one fragment. Inside the wrapper fragment you use getChildFragmentManager() to allow nested fragments.

Also if you want things in your navigation bar you should do this: https://stackoverflow.com/a/20189260/2767703.


This is of course a lot of work, so here is an alternative:

Most of the time fragments are the way to go with a navigation drawer, but if your app is getting quite large I suggest that you use activities instead.

To use activities I already wrote an answer somewhere else: https://stackoverflow.com/a/19451842/2767703.

This is my answer on how to use a navigation drawer on multiple activities and I think this is the best way to go if your app is large.

Community
  • 1
  • 1
Kevin van Mierlo
  • 9,554
  • 5
  • 44
  • 76