nephilimrising nephilimrising - 20 days ago 9
Android Question

Understanding android navigation drawer and fragments

i'm quite new to drawer material and i have trouble understanding some things:
I need to create an

Activity
with a
Fragment
on it. Different selections on the
Drawer
must replace the current
Fragment
with another one, but is the
Drawer
something in the fragment or it is one for the activity itself. More specifically is the
Drawer
living in the
Fragment
and if not is it possible to create it in the
Fragment
. I am asking this cause i see that when initiating the
Drawer
you need to fill parent activity. Also when i tried to use the
Navigation Drawer
template in
Android Studio
i didn't had the
Use a Fragment
checkbox.

Answer

There are many ways to achieve what you want. But I think the simplest way is:

1) DrawerLayout view should reside in the activity (probably as the base layout).

2) When you click on an item in the draw 2 things happen:

  • The fragment is replaced (you have one layout to contain the fragment and you just replace the fragment in it).

  • The items inside the drawer update (if you are making a list you would simply set the data and call notifyDataSetChanged().

Don't forget to save your state so it can recover in case the Activity is recreated.