2

Currently in my application i have a navigation drawer. When i began working on it i discovered that many people used fragments instead of activities, however, i needed to work with activities as i realised you weren't able to put fragments within fragments which i would need later on in development.

However, the issue is that by using activities, when i click on a new page from the navigation drawer there isn't a smooth animation as of course the activity has to load up again.

Is there any solution to this? or should i change the application to work with fragments. I have found that people do work with activities when using navigation drawer however i'm unsure if they have solved this issue or just work with it?

Thanks

Andy Joyce
  • 2,802
  • 3
  • 15
  • 24

2 Answers2

2

In your XML, when you create a NavigationDrawer you should specify a FrameLayout (or a ViewGroup I guess) that will essentially be the visible screen when the Drawer is not active. In order to actually use this, you have to use a Fragment approach by using the FragmentManager property of your DrawerActivity to swap out the current Fragment based on the list item selected from the Drawer. (I apologize if you already knew that)

Although it is not considered "best practice" but it is possible to nest fragments, see here OR here although you will need either android 4.2 or support library 11 which shouldn't be an issue.

In short, there is no way to use Activitys while maintaining the Navigation Drawer, you will have to use Fragments.

Community
  • 1
  • 1
Isaac Zais
  • 583
  • 4
  • 13
  • 1
    Thanks for answering that Isaac. Just following on from that, if i use fragments for each of of the menus navigations pages, if for example on that page there was a button to another page would it be better to do that page as a fragment or would i be able to use an activity? I presume that if i was to use an activity the menu would then no longer be there? Again thanks for the answer. – Andy Joyce Aug 20 '14 at 13:55
  • 1
    You are correct, if there is a button to go to another page you would have your fragment tell the activity to swap fragments to the one that you wish to present. As soon as you launch a new Activity, the NavigationDrawer is gone. – Isaac Zais Aug 20 '14 at 14:08
1

You should definitely work with Fragments. I don’t see why people would use activities at all. I guess the Android Developer Training page describes best how to use the NavigationDrawer with Fragments. (Click)

In terms of your concern with fragments within fragments. Since Android 4.2 nested Fragments are possible. The Android Support Library (rev11) added support for nested Fragments later on, so you can basically have nested fragment on every android version.

You can read more about Nested Fragments here: Click

reVerse
  • 35,075
  • 22
  • 89
  • 84