๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Kotlin

Navigation - jetpack / kotlin xml ์š”์†Œ ์ฐธ์กฐ / ์„œ์  ์ฐธ๊ณ  - Kotlin ๊ธฐ๋ณธ ๋ฌธ๋ฒ• ๊ณต๋ถ€ ์ผ์ง€

by ๋„์บ๋ฆฌ๐Ÿฑ 2021. 6. 29.
๋ฐ˜์‘ํ˜•

 

[โ˜… ๊ธฐ๋ณธ์ ์ธ ์ฐธ๊ณ ] kotlin xml ์š”์†Œ ์ฐธ์กฐ

https://itstudy-mary.tistory.com/340

plugins {
    ...
    id 'kotlin-android'
    id 'kotlin-android-extensions'
}

 

 

https://developer.android.com/guide/navigation/navigation-getting-started#java

 

ํƒ์ƒ‰ ๊ตฌ์„ฑ์š”์†Œ ์‹œ์ž‘ํ•˜๊ธฐ  |  Android ๊ฐœ๋ฐœ์ž  |  Android Developers

์ด ์ฃผ์ œ๋Š” ํƒ์ƒ‰ ๊ตฌ์„ฑ์š”์†Œ๋ฅผ ์„ค์ •ํ•˜๊ณ  ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ค๋ช…ํ•ฉ๋‹ˆ๋‹ค. ํƒ์ƒ‰ ๊ตฌ์„ฑ์š”์†Œ์˜ ๋Œ€๋žต์ ์ธ ๊ฐœ์š”๋Š” ํƒ์ƒ‰ ๊ฐœ์š”๋ฅผ ์ฐธ๊ณ ํ•˜์„ธ์š”. ํ™˜๊ฒฝ ์„ค์ • ์ฐธ๊ณ : ํƒ์ƒ‰ ๊ตฌ์„ฑ์š”์†Œ๋Š” Android ์ŠคํŠœ๋””์˜ค 3.3 ์ด์ƒ์ด ํ•„์š”

developer.android.com

 

0. [์„ค์ • ์‚ฌํ•ญ]ํ•œ ์•กํ‹ฐ๋น„ํ‹ฐ๋ฅผ ํ‹€ ์ฒ˜๋Ÿผ ๋‘๊ณ , ํ”„๋ ˆ๊ทธ๋จผํŠธ๋กœ ํ™”๋ฉด ์ „ํ™˜์„ ํ•  ์˜ˆ์ • (ํ”„๋ ˆ๊ทธ ๋จผํŠธ 4๊ฐœ - main, question, selection, result)

 

1. ์œ„์˜ ์‚ฌ์ดํŠธ์—์„œ dependancy์— ์•„๋ž˜ ์ถ”๊ฐ€ 

val nav_version = "2.3.5"

// Kotlin
  implementation("androidx.navigation:navigation-fragment-ktx:$nav_version")
  implementation("androidx.navigation:navigation-ui-ktx:$nav_version")

 

2. MainActivity์— "fragment"์š”์†Œ๋ฅผ ๋‘”๋‹ค. (์œ„ ๋ฌธ์„œ์—๋„ ์ž‘์„ฑ๋˜์–ด์žˆ์Œ) 

2-1 . ๋„ค๋น„๊ฒŒ์ด์…˜ ๊ธฐ๋ณธ ๋””๋ ‰ํ„ฐ๋ฆฌ๋ฅผ ๋งŒ๋“ ๋‹ค. ํ”„๋ ˆ๊ทธ๋จผํŠธ๋„ 4๊ฐœ (0๋ฒˆ์—์„œ ์„ค์ •ํ•œ) ๋งŒ๋“ ๋‹ค. ๋„ค๋น„๊ฒŒ์ด์…˜ ํŒŒ์ผ์„ ๋งŒ๋“ค๊ณ  4๊ฐœ์˜ ํ”„๋ ˆ๊ทธ๋จผํŠธ๋ฅผ ์„ธํŒ…ํ•œ๋‹ค. ๋“œ๋ž˜๊ทธ๋กœ ์ „ํ™˜ํ•˜๋Š” ๋ถ€๋ถ„์„ ์—ฐ๊ฒฐ ์ง“๋Š” ์‹์œผ๋กœ graph๋ฅผ ๋งŒ๋“ ๋‹ค.

<androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toBottomOf="parent"

        app:defaultNavHost="true"
        app:navGraph="@navigation/nav_graph" />

 

์œ„์—์„œ ์•„๋ž˜๋Š” NavHost๋ž€ NavController๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋Š” ์ธํ„ฐํŽ˜์ด์Šค์ด๋‹ค. 

app:defaultNavHost="true"

 

MainActivity์—์„œ๋Š” ์•„๋ž˜์™€ ๊ฐ™์ด ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ฐพ๋Š”๋‹ค. ์ด ์ปจํŠธ๋กค๋Ÿฌ๋Š” ๋Œ€์ƒ์œผ๋กœ ์ด๋™ํ•˜๋Š” ๊ฒƒ์€ NavController ๊ฐ์ฒด๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์‹คํ–‰๋˜๋ฉฐ ์ด ๊ฐ์ฒด๋Š” NavHost ๋‚ด์—์„œ ์•ฑ ํƒ์ƒ‰์„ ๊ด€๋ฆฌํ•œ๋‹ค.

lateinit var navController :NavController

navController = nav_fragment.findNavController()

 

3. Bundle๋กœ ๋ฐ์ดํ„ฐ ์ฃผ๊ณ  ๋ฐ›๊ฒŒ ํ•˜๊ธฐ

 

3-1. ๋ฐ์ดํ„ฐ ๋ณด๋‚ด๋Š” SelectionFragment

class SelectionFragment : Fragment() , View.OnClickListener{


    lateinit var navController: NavController

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_selection, container, false)
    }


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

        navController = Navigation.findNavController(view)

        btn_back.setOnClickListener(this)
        selection_1.setOnClickListener(this)
        selection_2.setOnClickListener(this)
        selection_3.setOnClickListener(this)
        selection_4.setOnClickListener(this)

    }


    override fun onClick(v: View?) {

        // ๋ฐ›์•„์˜จ ๋ทฐ๊ฐ€ null์ด ์•„๋‹ˆ๋ฉด id ๋ฅผ ๊ฐ€์ ธ์˜ค๊ณ , ๋งŒ์•ฝ view๊ฐ€ null์ด๋ผ๋ฉด ์•„๋ž˜ ์ „์ฒด when ์€ Null์„ ๋ฐ˜ํ™˜ํ•˜๊ฒŒ ๋œ๋‹ค.
        when(v?.id){
            R.id.selection_1 ->{ navigateWithIndex(1) }
            R.id.selection_2 ->{ navigateWithIndex(2) }
            R.id.selection_3 ->{ navigateWithIndex(3) }
            R.id.selection_4 ->{ navigateWithIndex(4) }
            R.id.btn_back->{
                /*** ํ”„๋ ˆ๊ทธ๋จผํŠธ๋Š” ์ด๋™์„ ํ•  ๋•Œ ํ•ญ์ƒ BackStack์— ํ”„๋ ˆ๊ทธ๋จผํŠธ๊ฐ€ ์Œ“์ž„. ๋’ค๋กœ๊ฐ€๊ธฐ๋Š” ๊ทธ๋ž˜์„œ ํ•˜๋‚˜์”ฉ ์Šคํƒ์— ์žˆ๋Š” ๊ฑธ ํŒํ•ด์ฃผ๋ฉด ๋˜๋Š” ๊ฒƒ*/
                navController.popBackStack()

            }

        }
    }


    // ํ”„๋ ˆ๊ทธ๋จผํŠธ๋กœ ๊ฐ’์„ ๋ณด๋‚ด์ฃผ๊ธฐ ์œ„ํ•ด์„œ, Bundle๋กœ ๊ฐ’์„ ๋ณด๋‚ด์ค„๊ฑฐ์ž„.
    fun navigateWithIndex(value : Int){

        // key - value
        val bundle = bundleOf("index" to value)
        navController.navigate(R.id.action_selectionFragment_to_resultFragment,bundle)
    }
}

3-2. ๋ฐ์ดํ„ฐ ๋ฐ›๋Š” resultFragment

class ResultFragment : Fragment() {

    // var option : Int? = -1
    var option = -1
    // ๋ฒˆ๋“ค ๋„˜์–ด์˜จ ๊ฐ’


    lateinit var navController: NavController

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

       //  option = arguments?.getInt("index") // argument๊ฐ€ null ์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ž„
        // arguments ๋กœ ๋ฒˆ๋“ค ๊ฐ’์ด ๋„˜์–ด ์˜ค๋Š” ๊ฒƒ.
        option = arguments?.getInt("index")?:-1 // argument๊ฐ€ null ์ผ ์ˆ˜ ์žˆ๋Š” ๊ฒƒ์ธ๋ฐ ?๋ฅผ ์‚ฌ์šฉํ•ด์„œ null์ผ ๋•Œ๋Š” -1์„ ๋ฆฌํ„ด

        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_result, container, false)

    }

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

        navController = Navigation.findNavController(view)
        setResult(option)


        back_home.setOnClickListener {
            navController.navigate(R.id.action_resultFragment_to_mainFragment)
        }
    }


    fun setResult(option : Int)
    {
        when(option){
            1 ->{
                // textView_sub.setText("") ๊ฐ™๋‹ค.
                textView_sub.text = "1 result"
            }
            2 ->{  textView_sub.text = "2 result"}
            3 ->{  textView_sub.text = "3 result"}
            4 ->{  textView_sub.text = "4 result"}
        }
    }
}

 

 

ํ”„๋ ˆ๊ทธ ๋จผํŠธ ๋ผ์ดํ”„ ์‚ฌ์ดํด

 


 

4. ํ•จ์ˆ˜

ํ•จ์ˆ˜ ํ—ค๋” - (์ƒ๋žต ๊ฐ€๋Šฅ)๊ฐ€์‹œ์„ฑ ์ œํ•œ์ž/ํ•จ์ˆ˜ ์„ ์–ธ ํ‚ค์›Œ๋“œ / ํ•จ์ˆ˜ ์ด๋ฆ„/ ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜ / ๋ฐ˜ํ™˜ํƒ€์ž…

               ์ฝ”ํ‹€๋ฆฐ ๊ธฐ๋ณธ ๊ฐ€์‹œ์„ฑ ์ œํ•œ์ž :  public  (private ์„ ์“ฐ๋ฉด ํ•ด๋‹น .kt ์•ˆ์—์„œ๋งŒ ํ•จ์ˆ˜ ์‚ฌ์šฉ)

               ํ•จ์ˆ˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋Š” ํ•ญ์ƒ ์ฝ๊ธฐ ์ „์šฉ - ์ฆ‰, ํ•จ์ˆ˜ ๋ชธ์ฒด ๋‚ด๋ถ€์—์„œ ๊ฐ’ ๋ณ€๊ฒฝ ํ•  ์ˆ˜ ์—†์œผ๋ฏ€๋กœ val์ž„.

 

ํ•จ์ˆ˜ ๋ฐ”๋”” - ํ—ค๋” ๋‹ค์Œ์— ์ค‘๊ด„ํ˜ธ { }๋ฅผ ์‚ฌ์šฉํ•ด์„œ ํ•จ์ˆ˜ ๋ชธ์ฒด๋ฅผ ์ •์˜ํ•œ๋‹ค.

 

โœฒ ์ง€์—ญ๋ณ€์ˆ˜์™€ ํŒŒ์ผ ์ˆ˜์ค€ ๋ณ€์ˆ˜๋Š” ์ดˆ๊ธฐํ™”๋œ ๊ฐ’์„ ์ง€์ •ํ•  ๋•Œ ์ฐจ์ด์ ์ด ์žˆ๋‹ค. ํŒŒ์ผ ์ˆ˜์ค€ ๋ณ€์ˆ˜๋Š” ํ•ญ์ƒ ์„ ์–ธ๋ ๋•Œ ์ดˆ๊นƒ๊ฐ’์ด ์ง€์ •๋˜์–ด์•ผ ํ•˜๋ฉฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ปดํŒŒ์ผ ์—๋Ÿฌ ๋ฐœ์ƒํ•œ๋‹ค.

 

๋‹จ์ผ ํ‘œํ˜„์‹ ํ•จ์ˆ˜ - ํ—ค๋”์— ์ •์˜ํ•˜๋Š” ๋ฐ˜ํ™˜ ํƒ€์ž…, ๋ชธ์ฒด๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ์ค‘๊ด„ํ˜ธ, ๋ชธ์ฒด ๋‚ด๋ถ€์˜ return ๋ฌธ์„ ๋ชจ๋‘ ์ƒ๋žตํ•  ์ˆ˜ ์žˆ๋‹ค. 

 

๋ฆฌํ„ด ํƒ€์ž… ์—†๋Š” ํ•จ์ˆ˜ - Unit (์ƒ๋žต ๊ฐ€๋Šฅ), Nothing

                             : ๋‘˜์˜ ์ฐจ์ด์  - Nothing ํƒ€์ž…์€ ํ•จ์ˆ˜์˜ ์‹คํ–‰์ด ๋๋‚˜๋”๋ผ๋„ ํ˜ธ์ถœ ์ฝ”๋“œ๋กœ ์ œ์–ด๊ฐ€ ๋ณต๊ท€๋˜์ง€ ์•Š๋Š”๋‹ค. Nothing ํƒ€์ž…์˜

                               ์šฉ๋„๋Š” ์˜๋„์ ์œผ๋กœ ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ฌ ๋•Œ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค. 

Todo ํ•จ์ˆ˜์— Nothing์ด ์‚ฌ์šฉ๋˜๋Š”๋ฐ, Todoํ•จ์ˆ˜๋Š” ์˜ˆ์™ธ๋ฅผ ๋ฐœ์ƒ์‹œํ‚ค๋ฉฐ Nothing ํƒ€์ž…์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

→ Todo๋ฅผ ์‚ฌ์šฉํ•  ๋•Œ : ์—ฌ์ „ํžˆ ํ•ด์•ผ ํ•  ๊ฒƒ์ด ์žˆ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๋ ค ์ฃผ๊ธฐ ์œ„ํ•ด ์‚ฌ์šฉ๋œ๋‹ค.

 

↓ ํ•จ์ˆ˜ ์˜ค๋ฒ„๋กœ๋”ฉ: ํ•จ์ˆ˜ ์ด๋ฆ„์€ ๊ฐ™์ง€๋งŒ ๋งค๊ฐœ๋ณ€์ˆ˜ ํƒ€์ž…์ด ์—ฌ๋Ÿฌ๊ฐœ์ธ

fun sum(a: Int b: Int) : Int {
	return a+b
}

fun sum(a: Double b: Double c: Double) : Double {
	return a+b+C
}

 

๋ฐฑํ‹ฑ ํ•จ์ˆ˜ ์ด๋ฆ„ 

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€