jeraldo jeraldo - 4 months ago 128x
Android Question

Navigation Drawer to switch activities instead of fragments

Is it possible to use a navigation drawer in android but instead of updating fragments, i would like to switch between activities as my means of navigation within the app.


Yes it is possible - it's what I did for my app. I already had a number of activities set up, and rather than convert them all to fragments, I wanted to tailor the navigation drawer to work across all of them. Unfortunately, it's not a quick workaround, so if you have the option of using fragments, I would go with that. But regardless here's how I did it:

Let's say I have 2 activities, both of which I want to have the Navigation Drawer. In the layout.xml for each, I specified a DrawerLayout with the appropriate list view to hold my navigation options. Essentially, the Navigation drawer is made every time I switch between activities, giving the appearance that it is persisting. To make life a lot easier, I took the common methods required to set up the navigation drawer and put them in their own class: That way my activities can use the same custom adapter, etc.

Within this class, I have the following:

  • configureDrawer() - this sets up the ActionBar, ActionBarDrawerToggle, and the required listeners
  • My custom array adapter (to populate the navigation options within the list)
  • The selectOptions() method, which

When you set up the navigation drawer within one of your activities, you just create a new navigationDrawer object and pass in the required layout parameters (like the drawerLayout, listView etc). Then you'd call confiugreDrawer():

        navigationDrawer = new NavigationDrawerSetup(mDrawerView, mDrawerLayout,
            mDrawerList, actionBar, mNavOptions, currentActivity);


currentActivity is passed in since the navigation drawer is tied to the activity you are on. You will have to use it when you set up the ActionBarDrawerToggle:

mDrawerToggle = new ActionBarDrawerToggle(currentActivity, // host Activity
        mDrawerLayout, /* DrawerLayout object */
        R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
        R.string.drawer_open, /* "open drawer" description for accessibility */
        R.string.drawer_close /* "close drawer" description for accessibility */

You will also need to use currentActivity when seeting up your arrayAdapter:

As for how to switch between activities via the navigation drawer, you can just set up new intents within your selectItem() method:

private void selectItem(int position) {

    // Handle Navigation Options
    Intent intent;
    switch (position) {
        case 0:
            intent = new Intent(currentActivity, NewActivity.class);
        case 1: 
            // etc.

Just make sure that your NewActivity also has the navigation drawer setup and it should display.

There are a ton of things you can do to customize this method to your own needs, but this is the general structure of how I did it. Hope this helps!