Navigation Drawer to switch activities instead of fragments

SleepNot

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.

David Crozier

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!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Navigation Drawer to switch between activities

From Dev

Type error - Switch fragments in the Navigation Drawer

From Dev

Android: how to convert activities to fragments in order to use the navigation drawer correctly?

From Dev

What are the advantages of using Navigation Drawer with Activities VS with Fragments?

From Dev

How to Switch Fragments by Navigation Drawer (new support library by android)?

From Dev

Android switch Fragments in the new design support navigation drawer

From Dev

Up navigation with fragments and activities

From Dev

Android - Navigation drawer fragments

From Dev

Switching fragments in navigation drawer

From Dev

Android - Navigation drawer fragments

From Dev

Switching fragments in navigation drawer

From Dev

Navigation Drawer Fragments

From Java

Same Navigation Drawer in different Activities

From Dev

Android Navigation Drawer implemented with Activities

From Dev

Android: Navigation drawer on multiple activities

From Dev

Share Navigation Drawer with multiple Activities

From Dev

TabLayout using activities instead of fragments

From Java

Why fragments, and when to use fragments instead of activities?

From Dev

Different toolbar for fragments and Navigation Drawer

From Dev

Navigation drawer with Activity and child Fragments

From Dev

Navigation Drawer implies nested fragments?

From Dev

Android Navigation Drawer with many fragments

From Dev

Android save Fragments into Navigation Drawer

From Dev

Set Fragments with TabLayout and Navigation Drawer

From Dev

Navigation drawer with Activity and child Fragments

From Dev

navigation drawer move between fragments

From Dev

Fragments not showing using navigation drawer

From Dev

Navigation drawer: unable to show the back icon instead of hamburger icon on the toolbar in the children activities

From Dev

Switch between Fragments with onNavigationItemSelected in new Navigation Drawer Activity template (Android Studio 1.4 onward)

Related Related

  1. 1

    Navigation Drawer to switch between activities

  2. 2

    Type error - Switch fragments in the Navigation Drawer

  3. 3

    Android: how to convert activities to fragments in order to use the navigation drawer correctly?

  4. 4

    What are the advantages of using Navigation Drawer with Activities VS with Fragments?

  5. 5

    How to Switch Fragments by Navigation Drawer (new support library by android)?

  6. 6

    Android switch Fragments in the new design support navigation drawer

  7. 7

    Up navigation with fragments and activities

  8. 8

    Android - Navigation drawer fragments

  9. 9

    Switching fragments in navigation drawer

  10. 10

    Android - Navigation drawer fragments

  11. 11

    Switching fragments in navigation drawer

  12. 12

    Navigation Drawer Fragments

  13. 13

    Same Navigation Drawer in different Activities

  14. 14

    Android Navigation Drawer implemented with Activities

  15. 15

    Android: Navigation drawer on multiple activities

  16. 16

    Share Navigation Drawer with multiple Activities

  17. 17

    TabLayout using activities instead of fragments

  18. 18

    Why fragments, and when to use fragments instead of activities?

  19. 19

    Different toolbar for fragments and Navigation Drawer

  20. 20

    Navigation drawer with Activity and child Fragments

  21. 21

    Navigation Drawer implies nested fragments?

  22. 22

    Android Navigation Drawer with many fragments

  23. 23

    Android save Fragments into Navigation Drawer

  24. 24

    Set Fragments with TabLayout and Navigation Drawer

  25. 25

    Navigation drawer with Activity and child Fragments

  26. 26

    navigation drawer move between fragments

  27. 27

    Fragments not showing using navigation drawer

  28. 28

    Navigation drawer: unable to show the back icon instead of hamburger icon on the toolbar in the children activities

  29. 29

    Switch between Fragments with onNavigationItemSelected in new Navigation Drawer Activity template (Android Studio 1.4 onward)

HotTag

Archive