๐ MainActivity์์ SearchFragment/FavoritesFragment ์ฌ๋ฆฌ๊ธฐ
navigation/nav_graph
<?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_search">
<fragment
android:id="@+id/navigation_search"
android:name="com.example.navgraph.SearchFragment"
android:label="search_fragment"
tools:layout="@layout/fragment_search">
<action
android:id="@+id/action_navigation_search_to_navigation_track_detail"
app:destination="@id/navigation_favorites" />
</fragment>
<fragment
android:id="@+id/navigation_favorites"
android:name="com.example.navgraph.FavoritesFragment"
android:label="favorites_fragment"
tools:layout="@layout/fragment_favorites">
<action
android:id="@+id/action_navigation_track_detail_to_navigation_search"
app:destination="@id/navigation_search" />
</fragment>
</navigation>
menu/nav_menu
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_search"
android:icon="@drawable/ic_search"
android:title="๋
ธ๋ ๊ฒ์" />
<item
android:id="@+id/navigation_favorites"
android:icon="@drawable/ic_favorites"
android:title="๋ด ๋ณด๊ดํจ" />
</menu>
<?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">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/nav_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
Navigation Component์ ๊ด๋ จ๋ ์ค์ํ ์ค์ 2๊ฐ์ง
1.app:defaultNavHost="true"
2.app:navGraph="@navigation/nav_graph"
- app:defaultNavHost="true"
: ์ด ์์ฑ์ ํ์ฌ FragmentContainerView๊ฐ ์ฑ ๋ด์ ๊ธฐ๋ณธ ๋ค๋น๊ฒ์ด์ ํธ์คํธ๋ก ์๋ํด์ผ ํจ์ ๋ํ๋
์ฆ, ์ฌ์ฉ์๊ฐ Android ๊ธฐ๊ธฐ์ ๋ฐฑ ๋ฒํผ์ ๋๋ฅด๊ฑฐ๋, ์ก์ ๋ฐ์ 'Up' ๋ฒํผ์ ์ฌ์ฉํ ๋, Navigation Component๊ฐ ์ ์ํ ๋๋ก ํ๋๊ทธ๋จผํธ ๊ฐ ํ์์ด ์ด๋ฃจ์ด์ง๋๋ค.- app:navGraph="@navigation/nav_graph"
: ์ด ์์ฑ์ FragmentContainerView์ ์ฌ์ฉํ ๋ค๋น๊ฒ์ด์ ๊ทธ๋ํ๋ฅผ ์ง์ ๋ฐ๋์ ๋์์ ์ฌ์ฉํ ํ์๋ ์์ง๋ง, ์ผ๋ฐ์ ์ผ๋ก ํจ๊ป ์ฌ์ฉ๋ฉ๋๋ค. ์ด ๋ ์์ฑ์ด ํจ๊ป ์๋ํ ๋, FragmentContainerView ๋๋ NavHostFragment๊ฐ ์ฑ ๋ด์ ๋ค๋น๊ฒ์ด์ ํ๋ฆ์ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ ์ ์๊ธฐ ๋๋ฌธ
class MainActivity : AppCompatActivity() {
private val binding: ActivityMainBinding by lazy {
ActivityMainBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
val navHostFragment = supportFragmentManager.findFragmentById(R.id.nav_host_fragment) as NavHostFragment
val navController = navHostFragment.navController
binding.bottomNavigation.setupWithNavController(navController)
}
}
<?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_search">
<fragment
android:id="@+id/navigation_search"
android:name="com.example.navgraph.SearchFragment"
android:label="search_fragment"
tools:layout="@layout/fragment_search">
<action
android:id="@+id/action_navigation_search_to_navigation_track_detail"
app:destination="@id/navigation_favorites" />
</fragment>
<fragment
android:id="@+id/navigation_favorites"
android:name="com.example.navgraph.FavoritesFragment"
android:label="favorites_fragment"
tools:layout="@layout/fragment_favorites">
<action
android:id="@+id/action_navigation_track_detail_to_navigation_search"
app:destination="@id/navigation_search" />
</fragment>
</navigation>
์์ ์ฝ๋์์ id, navigation_search์ navigation_favorites๊ฐ ์๋ ์ฝ๋์์ id๋ ๊ฐ์์ผ ํจ
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_search"
android:icon="@drawable/ic_search"
android:title="๋
ธ๋ ๊ฒ์" />
<item
android:id="@+id/navigation_favorites"
android:icon="@drawable/ic_favorites"
android:title="๋ด ๋ณด๊ดํจ" />
</menu>
android:name="androidx.navigation.fragment.NavHostFragment"
์ด๋
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
์
๋ ฅํด์ค์ผ ํจ