Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 981
SleepNot Avatar asked Oct 18 '13 05:10

SleepNot


People also ask

Can I use activity instead of fragment?

It can't exist independently. We can't create multi-screen UI without using fragment in an activity, After using multiple fragments in a single activity, we can create a multi-screen UI. Fragment cannot be used without an Activity.

How do I add a navigation drawer to an existing activity?

To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.


1 Answers

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 ListView 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: NavigationDrawerSetup.java. That way my activities can use the same custom adapter, etc.

Within this NavigationDrawerSetup.java 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 handles drawer item clicks

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

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

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 setting up your custom Adapter:

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);             intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);             currentActivity.startActivity(intent);             break;         case 1:              // etc.     } 

Just make sure that your new Activity 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!

like image 139
David Crozier Avatar answered Sep 21 '22 17:09

David Crozier