๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | Navigation

hyihyiยท2024๋…„ 3์›” 14์ผ
1

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
61/70
post-thumbnail

๐Ÿ“– Navigation

์•ฑ ๋‚ด์—์„œ ์‚ฌ์šฉ์ž๊ฐ€ ๋‹ค์–‘ํ•œ ํ™”๋ฉด(์•กํ‹ฐ๋น„ํ‹ฐ๋‚˜ ํ”„๋ž˜๊ทธ๋จผํŠธ) ์‚ฌ์ด๋ฅผ ์ด๋™ํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋„์™€์ฃผ๋Š” ์ค‘์š”ํ•œ ๊ตฌ์„ฑ ์š”์†Œ
๋ณดํ†ต ํ•˜๋‚˜์˜ Activity์—์„œ ์—ฌ๋Ÿฌ Fragment ์‚ฌ์ด๋ฅผ ์ด๋™ํ•  ๋•Œ ์“ด๋‹ค.
๋‹ค๋ฅธ Activity๋กœ ์ด๋™ํ•  ๋•Œ๋Š” Intent๋กœ ์ด๋™ํ•˜์ž.

๐Ÿ“ Single Activity Architecture

์ฃผ๋กœ ๋‹จ์ผ ์•กํ‹ฐ๋น„ํ‹ฐ ๋‚ด์—์„œ ์—ฌ๋Ÿฌ ํ”„๋ž˜๊ทธ๋จผํŠธ ๊ฐ„์˜ ์ด๋™์„ ์šฉ์ดํ•˜๊ฒŒ ํ•˜๋Š” ๋ฐ ์‚ฌ์šฉ
์ด๋ฅผ ํ†ตํ•ด ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์˜ ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋กœ์ง์„ ๋‹จ์ˆœํ™”ํ•˜๊ณ  ์ค‘์•™ ์ง‘์ค‘ํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.


๐Ÿ“– Navigation ์ปดํฌ๋„ŒํŠธ์˜ ์ฃผ์š” ๊ตฌ์„ฑ ์š”์†Œ

1. NavHost

์•ฑ์˜ MainActivity์— ๋ฐฐ์น˜๋œ๋‹ค. ๋Œ€๋ถ€๋ถ„์˜ ๊ฒฝ์šฐ NavHostFragment๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ํ†ตํ•ด ๋‚ด๋น„๊ฒŒ์ด์…˜์„ ๊ด€๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
์ผ๋ฐ˜์ ์œผ๋กœ NavHostFragment๋Š” ์•ฑ์˜ ๋ฉ”์ธ ์•กํ‹ฐ๋น„ํ‹ฐ ๋ ˆ์ด์•„์›ƒ์— ์ •์˜๋œ๋‹ค. ์ดํ›„ NavController ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด ํ”„๋ž˜๊ทธ๋จผํŠธ ๊ฐ„์˜ ์ด๋™(๋‚ด๋น„๊ฒŒ์ด์…˜)์„ ๊ด€๋ฆฌํ•œ๋‹ค.

2. NavController

๋‚ด๋น„๊ฒŒ์ด์…˜ ๋™์ž‘์„ ๊ด€๋ฆฌํ•˜๋Š” ๊ฐ์ฒด๋กœ, ์•ฑ ๋‚ด์—์„œ์˜ ๋‚ด๋น„๊ฒŒ์ด์…˜ ๋กœ์ง์„ ์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ๋ชฉ์ ์ง€๋กœ ์ด๋™ํ•  ๋•Œ NavController๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ด๋™์„ ์ง€์‹œํ•ฉ๋‹ˆ๋‹ค.

3. Navigation Graph

๋‚ด๋น„๊ฒŒ์ด์…˜ ํ๋ฆ„์„ ์‹œ๊ฐ์ ์œผ๋กœ ํ‘œํ˜„ํ•˜๋Š” XML ํŒŒ์ผ๋กœ, ์•ฑ ๋‚ด์˜ ๋ชจ๋“  ๋‚ด๋น„๊ฒŒ์ด์…˜ ๊ด€๋ จ ์ •๋ณด(๋ชฉ์ ์ง€, ์•ก์…˜, ์ „ํ™˜ ๋“ฑ)๋ฅผ ํฌํ•จํ•ฉ๋‹ˆ๋‹ค. Android Studio์˜ Navigation Editor๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‰ฝ๊ฒŒ ์ž‘์—…ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

4. Destinations

์‚ฌ์šฉ์ž๊ฐ€ ๋‚ด๋น„๊ฒŒ์ด์…˜์„ ํ†ตํ•ด ๋„๋‹ฌํ•  ์ˆ˜ ์žˆ๋Š” ์•ฑ ๋‚ด์˜ ๊ฐ ํ™”๋ฉด์„ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค. ์ด๋Š” ์•กํ‹ฐ๋น„ํ‹ฐ, ํ”„๋ž˜๊ทธ๋จผํŠธ ๋˜๋Š” ๋‹ค์ด์–ผ๋กœ๊ทธ ๋“ฑ์ด ๋  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

5. Actions

๋ชฉ์ ์ง€ ๊ฐ„์˜ ์—ฐ๊ฒฐ์„ ์ •์˜ํ•˜๋ฉฐ, ์‚ฌ์šฉ์ž๊ฐ€ ํ•œ ํ™”๋ฉด์—์„œ ๋‹ค๋ฅธ ํ™”๋ฉด์œผ๋กœ ์ด๋™ํ•  ๋•Œ ์‚ฌ์šฉ๋ฉ๋‹ˆ๋‹ค.

๐Ÿ“– ์ฝ”๋“œ

1. NavHostFragment ์„ค์ •ํ•˜๊ธฐ

<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:defaultNavHost="true"
    app:navGraph="@navigation/nav_graph" />

2. Navigation Graph ์ƒ์„ฑํ•˜๊ธฐ

<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nav_graph"
    app:startDestination="@id/loginFragment">

    <fragment
        android:id="@+id/loginFragment"
        android:name="com.example.myapp.LoginFragment"
        android:label="๋กœ๊ทธ์ธ ํ™”๋ฉด">
        <action
            android:id="@+id/action_loginFragment_to_mainFragment"
            app:destination="@id/mainFragment" />
    </fragment>

    <fragment
        android:id="@+id/mainFragment"
        android:name="com.example.myapp.MainFragment"
        android:label="๋ฉ”์ธ ํ™”๋ฉด" />
</navigation>

3. NavController๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ๋ชฉ์ ์ง€ ์ด๋™ํ•˜๊ธฐ

class LoginFragment : Fragment() {

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        return inflater.inflate(R.layout.fragment_login, container, false)
    }

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

        val loginButton = view.findViewById<Button>(R.id.login_button)
        loginButton.setOnClickListener {
            //๋กœ๊ทธ์ธ ์„ฑ๊ณต ํ›„ ๋ฉ”์ธ ํ™”๋ฉด์œผ๋กœ ์ด๋™
            findNavController().navigate(R.id.action_loginFragment_to_mainFragment)
        }
    }
}
profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€