-1

First question here, hope I'll get the help I need :) First of all I tried to check at this thread: Same Navigation Drawer in different Activities and a lot others and got nothing :\

Question:

So I make a app that supposed to be built like this:

Navigation Drawer - over all the activities.

Main page that contain information

Activity

I made the options on the drawer to go to another activities but:

  1. How do I make the drawer be visible on the other activities?
  2. How do I make an option that when I'm currently in an activity and I press in the drawer again on the button that send me to the same activity it will just close the drawer? there is any option to check if I'm right now in a specific activity? here is my main page with the drawer:

    public class Main_Page extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main__page);
            Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar);
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                    this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
            drawer.setDrawerListener(toggle);
            toggle.syncState();
    
            NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
            navigationView.setNavigationItemSelectedListener(this);
        }
    
        @Override
        public void onBackPressed() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            if (drawer.isDrawerOpen(GravityCompat.START)) {
                drawer.closeDrawer(GravityCompat.START);
            } else {
                super.onBackPressed();
            }
    
        }
    
        @SuppressWarnings("StatementWithEmptyBody")
        @Override
        public boolean onNavigationItemSelected(MenuItem item) {
            Intent Home=new Intent(Main_Page.this,Main_Page.class);
            Intent Settings = new Intent(Main_Page.this,Settings.class);
            int id = item.getItemId();
    
            if (id == R.id.Main_App)
                startActivity(Home);
            else if (id== R.id.nav_Settings)
                startActivity(Settings);
            else if (id == R.id.Nav_Log_Out) {
                        super.onBackPressed();
            }
    
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            drawer.closeDrawer(GravityCompat.START);
            return true;
        }
    }
    

Thank you !

Saurabh
  • 434
  • 1
  • 4
  • 12
Matan Cohen
  • 17
  • 1
  • 8
  • Activity is Flow.. you cant put the same Drawer on a different Activity you need to create a new drawer on the new Activity. Use One Activity and Fragments – Itzik Samara Mar 18 '17 at 20:18
  • Possible duplicate of [Same Navigation Drawer in different Activities](http://stackoverflow.com/questions/19451715/same-navigation-drawer-in-different-activities) – Simon K. Gerges Mar 18 '17 at 20:24

1 Answers1

2

It is not possible, you have two options

  1. Use single activity with a content frame layout, and replace this layout with different fragments
  2. Use multiple activities with multiple navigation drawer with each activity has its item marked as selected.

Finally both solutions has pros and cons, and it depends on you business logic and how complicated each section is

Simon K. Gerges
  • 3,097
  • 36
  • 34
  • There is no way to make the Drawer appear on every activity on the app? – Matan Cohen Mar 18 '17 at 20:26
  • You have to add it to all of them, and they will not be connected, you have to add it separately and mark the selected row for each activity, but this not give you the effect of replacing the content after selecting the drawer item – Simon K. Gerges Mar 18 '17 at 20:32
  • And how do I do Option no1? – Matan Cohen Mar 18 '17 at 20:37
  • Let a single main Activity, that contains a drawer view, with drawer part and the main part is a single empty FrameLayout, and convert all your screens to fragments, then onSelection of any item on the drawer, replace the content FrameLayout with the proper Fragment. – Simon K. Gerges Mar 18 '17 at 20:42
  • I'm really new to it.. there is any tutorial for that? – Matan Cohen Mar 18 '17 at 20:48
  • The official Drawer tut from google https://developer.android.com/training/implementing-navigation/nav-drawer.html – Simon K. Gerges Mar 18 '17 at 20:51