7

I was trying to explore the new navigation drawer that is standardized by Google. Finally we have the official code as well.

I was trying to understand the best way to implement the same in my app.

My Structure of the app is as follows

-MainActivity
----Featured Tab
----Favorites Tab

On Clicking of any Element in either Tab it will take me below

ContentActivity
----Info Tab
----Map Tab

Now what i am confused is all the above has to be placed into the new View for the Nav Drawer.

And As the Nav Drawer Structure is something like this

An Activity with a layout which contains navdraw-view and intern it contains content-layout and drawer layout. On clicking an option in the drawer should i replace the content layout all together or should i open a new Activity ?

Charles
  • 50,943
  • 13
  • 104
  • 142
Harsha M V
  • 54,075
  • 125
  • 354
  • 529

2 Answers2

3

As detailed in the documentation for Creating a Navigation Drawer under "Handle Navigation Click Events", it works using Fragments. When the user chooses an option you only replace the content Fragment, allowing the navigation drawer to stay where it is. If you follow the documentation then you should be fine.

If you've not yet encountered Fragments then it would be worth reading up on them in the documentation for Fragments. They're sort of like activities, with a few differences to allow Android to show multiple fragments at a time.

Nick
  • 11,475
  • 1
  • 36
  • 47
  • Lets say i have multiple tabs in the second options of the nav drawer. do i need to build the tabs dynamically to replace all the content from the screen one ? – Harsha M V May 22 '13 at 14:14
  • 1
    It shouldn't matter whether your second activity has tabs or not - each screen will be a separate fragment which you can configure with XML similar to a view. It might help to read up on Fragments generally, because the navigation drawer is just a special case use of fragments. – Nick May 22 '13 at 14:20
  • thanks i will read it. looks like we can have tabs inside fragments – Harsha M V May 22 '13 at 14:30
  • While the example uses a Fragment (switching out the content) - that is not necessarily required. You can do anything you want in the selectItem method (so, you could also start a new Activity if that is a preferred action). Bottom line, Fragments are used in the example, but are in no way required for this implementation. – Booger May 22 '13 at 14:40
  • Since the navigation drawer is part of the activity, wouldn't starting a new activity remove/reset the drawer? Which would look a bit messy. And you'd have to include it in every activity. I agree with you that you _can_ implement this without fragments, but I think it works best with them. Plus you can give a nicer experience on tablets. – Nick May 22 '13 at 15:15
  • I actually use Fragments in my own code for this exact scenario (but we use an Spinner in the ActionBar to control which Fragment is switched out). I do think it is a good idea, however, I don't think it is necessary for the Nav Drawer (and Fragments can be add complications if you are a new developer). – Booger May 22 '13 at 16:22
1

The answer is that you could do either, you could switch out the content, with a new Fragment, or launch a new Activity.

You are free to do whatever you want in the selectItem(position) method.

The official documentation uses Fragments in their example, but it is not required. You can do anything you want in this method (just start with a Toast, or Logcat message, to see what happens when the element is clicked).

Booger
  • 18,579
  • 7
  • 55
  • 72
  • 3
    I feel like sometimes the Google documentation is overly complicated - I think adding Fragments to this example makes it harder to follow. I also think using an externalized Array for the list items is overly complicated (for this example). – Booger May 22 '13 at 14:47
  • 1
    If I launch new activities, and I want to have the Navigation drawer again in those activities, how do I implement this without re-writing the navigation drawer display code again and again? I tried with a BaseActivity, and the other activities which then get called from the navigation drawer are subclasses from it. The problem is, if I then have a different layout for the new acticities, I do not have access to the navigation drawer layout. Do i need to include this navigation drawer layout in the activity layouts ..?? – fischer_zh Jul 28 '13 at 12:02
  • Start a new question about this. This is a new issue, not addressable in comments. – Booger Jul 28 '13 at 12:38
  • I just did, sorry... http://stackoverflow.com/questions/17908120/android-navigation-drawer-calling-activities-with-abstractmainactivity – fischer_zh Jul 28 '13 at 12:39