I am doing an app with login fragment and when I log in or out it should update menu items (for guest only login item, for logged users : home,favorites, logout)

When I login in ,

nothing changes only after second try to login (like first click only logged in)

When I logout it disable (home,fav,logout) but doesn't show up login item.

Here code and photos

[![enter image description here](https://i.stack.imgur.com/MEY7Y.png)](https://i.stack.imgur.com/MEY7Y.png)

after log out :

[![enter image description here](https://i.stack.imgur.com/G6KOl.png)](https://i.stack.imgur.com/G6KOl.png)

Code :

```
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

lateinit var toogle : ActionBarDrawerToggle
lateinit var displayed_email: TextView

private lateinit var database: DatabaseReference
private lateinit var mAuth: FirebaseAuth

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val navigationView: NavigationView
    navigationView = findViewById(R.id.nav_view)
    navigationView.itemIconTintList=null
    navigationView.setNavigationItemSelectedListener(this)

    val headerView: View
    headerView = navigationView.getHeaderView(0)

    displayed_email = headerView.findViewById(R.id.nav_header_name)

    //Dokonczyc jutro
    //Dodac tez fragmenty z logowaniem i rejestracja
    //Kazdy ma miec swoje view modele

    toogle = ActionBarDrawerToggle(this, drawerLayout,findViewById(R.id.toolbar), R.string.open_menu, R.string.close_menu)
    drawerLayout.addDrawerListener(toogle)
    toogle.syncState()

    if(savedInstanceState==null){
        supportFragmentManager.beginTransaction().replace(R.id.fragment_container, LoginFragment()).commit()

    }

    updateUI()
}

override fun onNavigationItemSelected(item: MenuItem): Boolean {

    when(item.itemId){
        R.id.nav_login_item -> {
            Toast.makeText(applicationContext, "Login clicked",Toast.LENGTH_SHORT).show()
        }

        R.id.nav_logout_item->{
                mAuth.signOut()
                updateUI()
        }

        R.id.nav_home_item->{
            Toast.makeText(applicationContext, "Home clicked",Toast.LENGTH_SHORT).show()
        }
        R.id.nav_fav_item->{
            Toast.makeText(applicationContext, "Favorite clicked",Toast.LENGTH_SHORT).show()
        }
    }

    drawerLayout.closeDrawer(GravityCompat.START)
    return true
}

fun updateUI(){
    println("Updating UI")
    mAuth = FirebaseAuth.getInstance()
    database = FirebaseDatabase.getInstance().reference

    val navigationView: NavigationView
    navigationView = findViewById(R.id.nav_view)
    val nav_menu  = navigationView.menu

    if(mAuth.currentUser!=null) {

        displayed_email.setText(mAuth.currentUser!!.email.toString())
        displayed_email.textSize = 18F

        nav_menu.findItem(R.id.nav_login_item).isVisible = false
        nav_menu.findItem(R.id.nav_home_item).isVisible = true
        nav_menu.findItem(R.id.nav_fav_item).isVisible = true
        nav_menu.findItem(R.id.nav_logout_item).isVisible = true
    }else{

            displayed_email.setText("Guest")
            displayed_email.textSize= 30F

            //Naprawic pokazywanie login ikony itemu jak sie wylogujesz etc
        //Problem jest w zmianie stanu visibility podczas aplikacji
        //Niewazne w ktora strone

        nav_menu.findItem(R.id.nav_login_item).isVisible = true
        nav_menu.findItem(R.id.nav_home_item).isVisible = false
        nav_menu.findItem(R.id.nav_fav_item).isVisible = false
        nav_menu.findItem(R.id.nav_logout_item).isVisible = false

    }

  }

 }

```