DrawerLayout - control visibility

1

When the app is run the first drawer presented is correct ie; comes with only one visible option item. This option when selected activates an activity that is normally executed but at the end of its execution the other options are not visible.

drawer_layout

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single"
        android:id="@+id/drawer_register" android:visible="true">
        <item
            android:id="@+id/nav_drawer_register"
            android:title="@string/drawer_register"
            android:icon="@drawable/ic_person_add_black_24dp" />
    </group>

    <item android:title="@string/drawer_app"
        android:id="@+id/drawer_app" android:visible="false">
        <menu>
            <item
                android:id="@+id/nav_drawer_app_login"
                android:title="@string/drawer_app_login"
                android:icon="@drawable/ic_fingerprint_black_24dp" />
            <item
                android:id="@+id/nav_drawer_app_logout"
                android:title="@string/drawer_app_logout"
                android:icon="@drawable/ic_exit_to_app_black_24dp" />
        </menu>
    </item>

</menu> 

Main Activity

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar,
            R.string.main_navigation_drawer_open,
            R.string.main_navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) 
    findViewById(R.id.nav_view);
    navigationView.getMenu().clear();
    navigationView.inflateMenu(R.menu.activity_main_teste);
    navigationView.setNavigationItemSelectedListener(this);
    drawer.openDrawer(GravityCompat.START);

Listener

public boolean onNavigationItemSelected(MenuItem item) {

    int id = item.getItemId();

    // ================================================================================== //
    //                                FUNCTION REGISTER ME                                //
    // ================================================================================== //

    if (id == R.id.nav_drawer_register) {
        Toast toast =
                Toast.makeText(this,
                        R.string.device_register_warning, Toast.LENGTH_SHORT);
        toast.show();
        userType = "0"; 
        Intent intent = new Intent(RegisterStatusListActivity.ACTION_REG_STATUS_DATA);
        intent.addCategory(RegisterStatusListActivity.CATEGORY_REG_STATUS_DATA);
        intent.putExtra(RegisterStatusListActivity.EXTRA_REG_TYPE, userType);
        startActivity(intent);
        setAllVisible(true);       // <===== ATIVA MUDANÇA DE VISIBILIDADE
        return true;
    } else

setAllVisible

public void setAllVisible(boolean visible){

        NavigationView navigationView = (NavigationView) 
        findViewById(R.id.nav_view);
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.activity_main_teste);
        toggleVisibility(navigationView.getMenu(), R.id.drawer_app, visible);
    }

    private void toggleVisibility(Menu menu, @IdRes int id, boolean visible){
        menu.findItem(id).setVisible(visible);
}

SUMMARY

At the end of the activity that processed the first selected option, the drawer continues with the other invisible options. Is there a solution?

    
asked by anonymous 08.07.2016 / 16:17

1 answer

1

Make two menus and inflate them according to need, whether logged in or not.

Example:

Create these two menus in the menu directory

menu_logged.xml

   <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_login"
            android:icon="@drawable/ic_menu_login"
            android:title="Login" />
    </group>


</menu>

menu_deslogado.xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Import" />
        <item
            android:id="@+id/nav_gallery"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Gallery" />
        <item
            android:id="@+id/nav_slideshow"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Slideshow" />
        <item
            android:id="@+id/nav_logout"
            android:icon="@drawable/ic_menu_logout"
            android:title="Logout" />
    </group>

</menu>

Then in Java, you inflate them with an if, so

NavigationView navigationView = (NavigationView) findViewById (R.id.nav_view);

if(islogin)
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.menu_logado);
    } else
    {
        navigationView.getMenu().clear();
        navigationView.inflateMenu(R.menu.menu_deslogado);
    
08.07.2016 / 16:56