Search code examples
androidandroid-studioandroid-fragmentskotlinbottomnavigationview

how to I call fragment from fragment with bottomnavigationview - Kotlin


I have got problems.

There are 5 fragment in MainActivity. It is D1, D2, D3, D4, D5 Fragment. And there is BottomNavigationView in MainActivity. I want to call DxFragment from D1fragment. The button will be clicked and only the frame will change. But the button navigation bar will not change.

I've been looking for a while. I couldn't find the solution. Can anyone please help?

MAINACTIVITY

class MainActivity : AppCompatActivity() {
    private lateinit var bottomNavigationView : BottomNavigationView
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        bottomNavigationView = findViewById(R.id.nav_view)

        navigateFragment(false)

    }
    public fun loadFragment (fragment: Fragment){
        supportFragmentManager.beginTransaction().also { fragmentTransaction ->
            fragmentTransaction.replace(R.id.bottom_nav_host_fragment, fragment)
            fragmentTransaction.commit()
        }
    }

    public fun navigateFragment ( stateFragment : Boolean) {
            bottomNavigationView = findViewById(R.id.nav_view)

            bottomNavigationView.setOnNavigationItemSelectedListener { menuItem : MenuItem ->
                when{
                    menuItem.itemId== R.id.navigation_home -> {
                        loadFragment(D1Fragment())
                        return@setOnNavigationItemSelectedListener true
                    }
                    menuItem.itemId == R.id.navigation_map -> {
                        loadFragment(D2Fragment())
                        return@setOnNavigationItemSelectedListener true
                    }
                    menuItem.itemId == R.id.navigation_userpage -> {
                        loadFragment(D3Fragment())
                        return@setOnNavigationItemSelectedListener true
                    }
                    menuItem.itemId == R.id.navigation_fav -> {
                        loadFragment(D4Fragment())
                        return@setOnNavigationItemSelectedListener true
                    }
                    menuItem.itemId == R.id.navigation_list -> {
                        loadFragment(D5Fragment())
                        return@setOnNavigationItemSelectedListener true
                    }
                    stateFragment == true -> {
                        loadFragment(BlankFragment())
                        return@setOnNavigationItemSelectedListener true
                    }

                    else -> {
                        return@setOnNavigationItemSelectedListener true
                    }

                }
            }


    }

    override fun onSupportNavigateUp(): Boolean {
        return Navigation.findNavController(this, R.id.bottom_nav_host_fragment).navigateUp() ||
                super.onSupportNavigateUp()
    }

}

ACTIVITY_MAIN.XML some part

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="0dp"
    android:layout_marginEnd="0dp"
    android:background="?android:attr/windowBackground"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:menu="@menu/bottom_menu" />

<FrameLayout
    android:id="@+id/bottom_nav_host_fragment"
    android:name="androidx.navigation.fragment.NavHostFragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"

    app:layout_constraintBottom_toTopOf="@id/nav_view"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    />

FRAGMENT_D1.XML

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".D1Fragment">



    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="CLICK AND GO BLANKFRAGMENT" />

</FrameLayout>

D1FRAGMENT

class D1Fragment : Fragment() {

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment



    return inflater.inflate(R.layout.fragment_d1, container, false)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {



    button.setOnClickListener {

        Toast.makeText(context , "CLICKED" , Toast.LENGTH_SHORT ).show()
        Navigation.findNavController(view).navigate(R.id.action_d1Fragment_to_blankFragment)


    }
    super.onViewCreated(view, savedInstanceState)
}


}

enter image description here

BUTTON CLICK AND ERROR

java.lang.IllegalStateException: View android.widget.FrameLayout{16424ce5 V.E..... ........ 0,0-1080,1365} does not have a NavController set
    at androidx.navigation.Navigation.findNavController(Navigation.java:84)
    at com.axisting.bottomelledeneme.D1Fragment$onViewCreated$1.onClick(D1Fragment.kt:35)

Solution

  • You only needs to have a principal class,

    Example main's activity, it contains the BottomNavigationView and fragment. the fragments have the other element

    Example

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
    
    
        <BottomNavigationView
            android:id="@+id/bottom_navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:menu="@menu/bottom_nav_menu" />
    
    </androidx.con straintlayout.widget.ConstraintLayout>
    

    You only need to attach the fragment