마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

BottomNavigationView Action 질문 - 코틀린

0 추천

안녕하세요. 바텀네비게이션 Action 관련 질문입니다.

먼저 Bottom Navigation은 SettingFragment, DashBoardFragment, NotificationFragment로 설정되어 있는 상태입니다.

SettingFragment 화면에서 버튼을 누르면 DashBoardFragment로 이동하게 설계를 하였는데요.
문제점은, 이후에 바텀네비게이션에서 SettingFragment 항목을 누르면 해당 항목으로 이동되지 않는 현상입니다. 어떤 문제가 있는 것일까요?

MainActivity 

val navHostFragment =
    supportFragmentManager.findFragmentById(R.id.nav_host_fragment_activity_main) as NavHostFragment
val navController = navHostFragment.navController
findViewById<BottomNavigationView>(R.id.nav_view)
    .setupWithNavController(navController)

fun move() {
    val navController = findNavController(R.id.nav_host_fragment_activity_main)

    navController.navigate(R.id.action_navigation_setting_to_navigation_dashboard)
}

Navigation.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/mobile_navigation"
    app:startDestination="@+id/navigation_setting">

    <fragment
        android:id="@+id/navigation_setting"
        android:name="com.nex.nmp.ui.setting.SettingFragment"
        android:label="@string/title_setting"
        tools:layout="@layout/fragment_setting">
        <action
            android:id="@+id/action_navigation_setting_to_navigation_dashboard"
            app:destination="@id/navigation_dashboard" />

    </fragment>

    <fragment
        android:id="@+id/navigation_dashboard"
        android:name="com.nex.nmp.ui.dashboard.DashboardFragment"
        android:label="@string/title_dashboard"
        tools:layout="@layout/fragment_dashboard" />

    <fragment
        android:id="@+id/navigation_notifications"
        android:name="com.nex.nmp.ui.notifications.NotificationsFragment"
        android:label="@string/title_notifications"
        tools:layout="@layout/fragment_notifications" />
</navigation>
flasas (120 포인트) 님이 2022년 3월 22일 질문

1개의 답변

0 추천

네비게이션 설정 부분을 다시 확인해보세요. 아래와 같은 형태로 되는게 일반적입니다.

val navController = findNavController(R.id.nav_host_fragment_activity_main)


// NavigationComponent와 BottomNavigationView을 연결시키려면 아래 부분이 필요함.
val appBarConfiguration = AppBarConfiguration(
            setOf(
                R.id.navigation_setting, R.id.navigation_dashboard, R.id.navigation_notifications
            )
)

setupActionBarWithNavController(navController, appBarConfiguration) 
navView.setupWithNavController(navController)

 

그리고 Fragment에서는 findNavController() extension function을 사용하시면 되기 때문에 move()같은 함수가 MainActivity에 있을 필요가 없습니다.

spark (227,470 포인트) 님이 2022년 3월 23일 답변
...