Android How to implement a navigation drawer menu in all the activities?

3

Good morning.

I have a Navigation Drawer ready, but what I want to do is to be able to use it in any of my activities.

How could I implement this?

Thank you in advance.

    
asked by devjav 13.09.2016 в 18:21
source

3 answers

4

If you want to implement it in several activities you would have to make a copy in each Activity, which would not be a good idea.

I start to think how you would have structured your application, so you comment you have several Activity, remember that if you have several you could be making a stack of them in memory which can cause problems.

The right thing to do is to have a main Activity that contains the Navigation Drawer and inside you could Perform a Fragments transaction to add the one you want, instead of loading Activities.

Within your Activity you would make a change of fragment content via FragmentTransaction :

  FragmentManager fragmentManager = getSupportFragmentManager();
  FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();      
  //Se crea una instancia del fragmento a agregar dentro de la Activity.
  Fragment fragment = new myFragment();
  //Se agrega al contenedor.
  fragmentTransaction.add(R.id.fragment_container, fragment);
  fragmentTransaction.commit();
    
answered by 13.09.2016 / 20:30
source
2

For this case the recommendation is to use Fragments as they allow you to reuse the same activity (which includes the drawer) changing the content within the same life cycle of a single activity, with which the logic of the drawer is applied only once.

The Android documentation clarifies how to implement the drawer with this configuration, and if While it is possible to make a hack using inheritance and includes views to use activities, it is strongly recommended that you use fragments as indicated in this guide.

    
answered by 13.09.2016 в 19:24
0

One question, I have already done in navigation drawer (created as a new navigation drawer activity) and I have a fragment for each option, but what I do not know is how to make an initial window, that is, when the user login session appears and also this menu ... I do not know if modifying any xml file that generates the navigation drawer I can use one as the main view and the fragments of the menu.

    
answered by 02.04.2018 в 11:28