Simple&Natural

[Android] BottomNavigationView + Nav Component 연동 시 유의사항 정리 본문

안드로이드(Android)/기타

[Android] BottomNavigationView + Nav Component 연동 시 유의사항 정리

Essense 2020. 10. 10. 02:10
728x90

 

menu의 아이템과 nav_graph의 fragment id가 반드시 일치해야 서로 연동된다.

fragment의 경우 activity추가 시 반드시 supportFragmentManager를 통해 컨트롤러를 등록해야 함.

 

해당 이슈 참고

stackoverflow.com/questions/59275009/fragmentcontainerview-using-findnavcontroller/59275182?noredirect=1

 

FragmentContainerView using findNavController

I'm using Android Navigation Component with bottom navigation, lint gives a warning about replacing the tag with FragmentContainerView but when i replaced, findNavController is not

stackoverflow.com

www.xspdf.com/resolution/50502269.html

 

IllegalStateException: Link does not have a NavController set

Fragmentcontainerview does not have a navcontroller FragmentContainerView as NavHostFragment, Seems like using the FragmentContainerView doesn't work right out of the box? <​androidx.fragment.app.FragmentContainerView class="  Caused by: java.lang.Illeg

www.xspdf.com

 

 

 

bottom_navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/Home"
        android:title="@string/title_home"
        android:icon="@drawable/ic_baseline_home_24"/>
    <item
        android:id="@+id/Notification"
        android:title="@string/title_notification"
        android:icon="@drawable/ic_baseline_notifications_active_24"/>
    <item
        android:id="@+id/News"
        android:title="@string/title_news"
        android:icon="@drawable/ic_baseline_library_books_24"/>
    <item
        android:id="@+id/Setting"
        android:title="@string/title_setting"
        android:icon="@drawable/ic_baseline_settings_24"/>
</menu>

 

 

nav_graph.xml

<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="@+id/nav_graph"
    app:startDestination="@id/Home">
    <fragment
        android:id="@+id/Home"
        android:name="kr.co.douchgosum.android.coinradar.Main1Fragment"
        android:label="fragment_main1"
        tools:layout="@layout/fragment_main1" />
    <fragment
        android:id="@+id/Notification"
        android:name="kr.co.douchgosum.android.coinradar.Main2Fragment"
        android:label="fragment_main2"
        tools:layout="@layout/fragment_main2" />
    <fragment
        android:id="@+id/News"
        android:name="kr.co.douchgosum.android.coinradar.Main3Fragment"
        android:label="fragment_main3"
        tools:layout="@layout/fragment_main3" />
    <fragment
        android:id="@+id/Setting"
        android:name="kr.co.douchgosum.android.coinradar.Main4Fragment"
        android:label="fragment_main4"
        tools:layout="@layout/fragment_main4" />
</navigation>

 

 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<layout
    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">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <fragment
            android:id="@+id/nav_host"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph"/>

<!--        <androidx.fragment.app.FragmentContainerView-->
<!--            android:id="@+id/nav_host"-->
<!--            android:name="androidx.navigation.fragment.NavHostFragment"-->
<!--            app:navGraph="@navigation/nav_graph"-->
<!--            app:defaultNavHost="true"-->
<!--            android:layout_width="match_parent"-->
<!--            android:layout_height="0dp"-->
<!--            android:layout_weight="1"/>-->


        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_nav"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/bottom_navigation_menu" />

    </LinearLayout>
</layout>

 

MainActivity.class

class MainActivity : AppCompatActivity() {

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

//        val navController = findNavController(R.id.nav_host)
//        bottom_nav.setupWithNavController(navController)
        val navController = supportFragmentManager.findFragmentById(R.id.nav_host)?.findNavController()
        navController?.let {
            bottom_nav.setupWithNavController(navController)
        }
        
        
        ...
        
        // 공식문서의 예제코드는 다음과 같다. 편한 걸 쓰면 된다.
        val navHostFragment =
            supportFragmentManager.findFragmentById(R.id.nav_host) as NavHostFragment
        val navController = navHostFragment.navController
        val appBarConfiguration = AppBarConfiguration(navController.graph)
        toolbar
            .setupWithNavController(navController, appBarConfiguration)
        bottom_nav
            .setupWithNavController(navController)
            
            
            
        ...
        
        
      
        
    }

 

 

 

728x90