简体   繁体   中英

Create a Navigation drawer to link to fragments and activities

So i have a navigation drawer like so it has 3 buttons that go to 2 different fragments and the other button which goes to an Activity.

样本

When I click one frag 1 my fragment opens up with the drawer still intact same goes for Frag 2 but when i click on the Activity 1 the Drawer disappears but i would like the drawer to continue in the activity as well. can this be done.

What you'd want to do (roughly) is

  1. Establish a menu as one of your resources and establish the items in that list.
  2. In the activity that you want to contain the drawer, create the drawer object and create a callback for the onMenuItemSelected.
  3. In that callback, reference the menu item ids you created before and use intents and fragment managers to either start the activity or fragment that you want based on what they select.

This can't be done.

The DrawerLayout lives within your Activity and the Fragments that you switch to also live within the same Activity . This is the reason why switching fragments will leave the drawer intact. It's because they both exist within the same Activity without any interference.

However, launching an Activity is different. This is a completely different Activity which has it's own layout.

You actually only have two options if you wish to continue using a Drawer for main navigation.

  1. Remove the need for the second Activity and change that to a Fragment . This way, all your fragments will exist within the same parent Activity so it'll use the same drawer that exists in that parent Activity.

  2. Create an identical DrawerLayout and NavigationView in the second Activity. Call code to have the Drawer be opened when it's created. This way, although you're not really using the same Drawer, you're giving the illusion that it's still the same Drawer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM