Home > Net >  Fragment doesn't change after item click Navigation Drawer in Kotlin
Fragment doesn't change after item click Navigation Drawer in Kotlin

Time:06-07

I'm building an app which contains a NavigationDrawer to navigate between Fragments. For some reason, when pressing on any item in the Navigation Drawer the item just marks itself and doesn't change the fragment (tried debug but the item clicks doesn't calls the functions)

The fragment is not loading in first click..i have to press twice to reload the fragment

Main Activity Code:-

@file:Suppress("DEPRECATION")

package com.example.bottomnavbar

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ImageView
import android.widget.PopupMenu
import android.widget.Toast
import androidx.fragment.app.Fragment
import com.google.android.material.bottomnavigation.BottomNavigationView
import java.lang.Exception

class MainActivity : AppCompatActivity() {

    lateinit var bottomNav : BottomNavigationView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        loadFragment(HomeFragment())
        bottomNav = findViewById(R.id.bottomNav)
        bottomNav.setOnNavigationItemReselectedListener { task->
            when (task.itemId) {
                R.id.home -> {
                    loadFragment( HomeFragment())
                    return@setOnNavigationItemReselectedListener
                }
                R.id.message -> {
                    loadFragment(ChatFragment())
                    return@setOnNavigationItemReselectedListener
                }
                R.id.settings -> {
                    loadFragment(SettingFragment())
                    return@setOnNavigationItemReselectedListener
                }
            }
        }
    }
    private fun loadFragment(fragment: Fragment){
        val transaction = supportFragmentManager.beginTransaction()
        if(fragment!=null) {
            transaction.replace(R.id.container, fragment)
            transaction.addToBackStack(null)
            transaction.commit()
        }else{
            Toast.makeText(this,"No fragment",Toast.LENGTH_LONG).show()
        }
    }
}

XML Code:-

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <androidx.fragment.app.FragmentContainerView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:id="@ id/container"
        app:layout_constraintTop_toTopOf="parent"/>
    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        android:id="@ id/bottomNav"
        app:menu="@menu/bottommenu"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

CodePudding user response:

setOnNavigationItemReselectedListener I think you clearly mentioned in your code to only navigate if the item is reselected instead use setOnNavigationItemSelectedListener

  • Related