Search code examples
androidandroid-fragmentssharedpreferencesmaterialdrawer

Dynamically add Fragments in NavigationDrawer


I was using NavigationDrawer from android.support:design library, and it worked fine, but now i have swithed to MaterialDrawer by mikepenz, due to it's obvious features. But now i am stuck.

I want to add Fragment dynamically like i did with android.support:design library, in android.support:design library, i used SharedPreferences, to add/remove Fragments from settings like this:

mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(MenuItem menuItem) {
       mDrawerLayout.closeDrawers();
       if (savedPreferences.getBoolean("add_frag", true)) {
             if (menuItem.getTitle() == "frag") {
             FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
             xfragmentTransaction.replace(R.id.containerView, new fragFragment()).commit();
       }
    }
}

Now how can i do the same with MaterialDrawer?

There are many ways to change fragment by switch statement or using something like this:

if (position == 0) {                      
    FragmentTransaction fragmentTransaction = mFragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.containerView, new fragFragment()).commit();
}

But these statement, doesn't help in adding fragment using SharedPreferences. If i try SharedPreferences on above statement Fragment won't change, and this quite obvious.

Please HELP!


Solution

  • The code looks very similar to the one you have currently.

    First you build your drawer, and add some items, and add a listener. This listener allow you to execute whatever function you need:

    //Create the drawer
    new DrawerBuilder()
        .withActivity(this)
        .withToolbar(toolbar)
        .withAccountHeader(headerResult) //set the AccountHeader we created earlier for the header
        .addDrawerItems(
                new PrimaryDrawerItem().withName("Item 1").withIdentifier(1),
                new PrimaryDrawerItem().withName("Item 2").withIdentifier(2)
        )
        .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
            @Override
            public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                if (drawerItem != null) {
                    if (drawerItem.getIdentifier() == 1) {
                        //do what you want to do for the first item
                    } else if (drawerItem.getIdentifier() == 2) {
                        //do whatever you want to do for the second item
                    }
                }
    
                return false;
            }
        })
        .withSavedInstance(savedInstanceState)
        .build();
    

    There may be some other confusion on your side. SharedPreferences store data in your application. This has not really much to do with the MaterialDrawer in general, nor with Fragments