๊พธ์ค€ํ•จ์ด ์ง„๋ฆฌ๋‹ค!!

์–ด์ œ๋ณด๋‹ค ๋ฐœ์ „ํ•œ ์˜ค๋Š˜์ด ๋˜๊ณ ํ”ˆ ๐Ÿง‘๐Ÿปโ€๐Ÿ’ป ์˜ ๋ธ”๋กœ๊ทธ

Android/Kotlin

[Kotlin][Android] ์•ˆ๋“œ๋กœ์ด๋“œ Fragment(ํ”„๋ž˜๊ทธ๋จผํŠธ)์˜ ์ƒ๋ช…์ฃผ๊ธฐ ๋ฐ ์‚ฌ์šฉ๋ฐฉ๋ฒ•

๋ށ์š” 2022. 10. 20. 21:33

์•ˆ๋…•ํ•˜์„ธ์š” ๐Ÿ‘‹

์˜ค๋Š˜์€ ์•ˆ๋“œ๋กœ์ด๋“œ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋Œ€ํ•˜์—ฌ 

๊ณต๋ถ€ํ•œ ๋‚ด์šฉ์„ ํฌ์ŠคํŒ…ํ•ด๋ณด๋ ค ํ•ฉ๋‹ˆ๋‹ค.

 

Fragment(ํ”„๋ž˜๊ทธ๋จผํŠธ)๋ž€?

ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ์ž์ฒด ์ˆ˜๋ช… ์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ , ์ž์ฒด ์ž…๋ ฅ ์ด๋ฒคํŠธ๋ฅผ ๋ฐ›์œผ๋ฉฐ, ์•กํ‹ฐ๋น„ํ‹ฐ ์‹คํ–‰ ์ค‘์— ์ถ”๊ฐ€ ๋ฐ ์ œ๊ฑฐ๊ฐ€ ๊ฐ€๋Šฅํ•œ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ๋ชจ๋“ˆ์‹ ์„น์…˜์ด๋ผ๊ณ  ์ƒ๊ฐํ•˜๋ฉด ๋ฉ๋‹ˆ๋‹ค(๋‹ค๋ฅธ ์•กํ‹ฐ๋น„ํ‹ฐ์— ์žฌ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” "ํ•˜์œ„ ์•กํ‹ฐ๋น„ํ‹ฐ"์™€ ๊ฐ™์€ ๊ฐœ๋…).


Fragment(ํ”„๋ž˜๊ทธ๋จผํŠธ)์˜ ์ƒ๋ช…์ฃผ๊ธฐ

ํ”„๋ž˜๊ทธ๋จผํŠธ๋„ ์•กํ‹ฐ๋น„ํ‹ฐ์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉฐ ์•กํ‹ฐ๋น„ํ‹ฐ์— ์ข…์†๋˜์–ด ์žˆ๋Š” ๋งŒํผ ์•กํ‹ฐ๋น„ํ‹ฐ์˜ ์ƒ๋ช…์ฃผ๊ธฐ์™€

๊ด€๋ จ์ด ๊นŠ์Šต๋‹ˆ๋‹ค.

ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ

์œ„ ์‚ฌ์ง„์€ ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” ๋‹ค์ด์–ด๊ทธ๋žจ์ž…๋‹ˆ๋‹ค.

 

๊ธฐ๋ณธ์ ์œผ๋กœ ํ”„๋ž˜๊ทธ๋จผํŠธ๋„ Create , Start , Pause , Stop , Destroy๊ฐ€ ์กด์žฌํ•˜์ง€๋งŒ ์กด์žฌํ•˜์ง€๋งŒ

ํ”„๋ž˜๊ทธ๋จผํŠธ ๋‚ด๋ถ€ view์˜ ์ƒํƒœ์— ๋”ฐ๋ผ ์ƒ๋ช…์ฃผ๊ธฐ๊ฐ€ ์ถ”๊ฐ€๋กœ ์กด์žฌํ•ฉ๋‹ˆ๋‹ค. (onCreateView() , onViewCreated() ๋“ฑ)

 

๊ทธ๋Ÿผ (์ž์ฃผ ์‚ฌ์šฉ๋˜๋Š”?)ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ์ƒ๋ช…์ฃผ๊ธฐ์— ๋”ฐ๋ฅธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜๋“ค์— ๋Œ€ํ•˜์—ฌ ์•Œ์•„๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿ˜Ž


onAttach()

ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ํ”„๋ž˜๊ทธ๋จผํŠธ๋งค๋‹ˆ์ €์— ์ถ”๊ฐ€๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜์ž…๋‹ˆ๋‹ค.

 

onCreate()

ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ์ƒ์„ฑ์‹œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

 

onCreateView()

ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋ฐฐ์น˜ํ•  View๋ฅผ ์ƒ์„ฑํ•ด ๋‹ฌ๋ผ๊ณ  ์š”์ฒญํ•˜๊ธฐ ์œ„ํ•ด ํ˜ธ์ถœ๋˜๋ฉฐ 

์—ฌ๊ธฐ์„œ Fragment์— ๋ฐฐ์น˜ํ•  View๋ฅผ ์ƒ์„ฑํ•˜์—ฌ ๋„˜๊ฒจ์ค๋‹ˆ๋‹ค.

 

onViewCreated()

Fragment์— ๋ฐฐ์น˜ํ•  View๊ฐ€ ์ƒ์„ฑ๋˜์—ˆ์„ ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

 

onActivityCreated()

Activity์˜ onCreate๊ฐ€ ์™„๋ฃŒ๋œ ๋‹ค์Œ ํ˜ธ์ถœ๋˜๋ฉฐ, ์ด๋•Œ Fragment์˜ View๋Š” Activity ์— ์ถ”๊ฐ€๋˜์–ด ์žˆ๋Š” ์ƒํƒœ๊ฐ€ ๋ฉ๋‹ˆ๋‹ค.


onDestroyView()

Fragment์—์„œ onCreateView๋กœ ์ƒ์„ฑํ•œ View๋ฅผ ์ œ๊ฑฐํ•  ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.

 

onDestroy()

ํ”„๋ž˜๊ทธ๋จผํŠธ๊ฐ€ ๋” ์ด์ƒ ์‚ฌ์šฉ๋˜์ง€ ์•Š์„ ๋•Œ ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.



๋งˆ์ง€๋ง‰์œผ๋กœ ํ”„๋ž˜๊ทธ๋จผํŠธ์˜ ๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋ฅผ ๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๐Ÿค”

 

acitivty_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:weightSum="10"
    android:padding="16dp"
    android:orientation="vertical"
    >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:weightSum="2"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_weight="1"
        >
        <Button
            android:id="@+id/btn_first"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="์ฒซ๋ฒˆ์งธ"
            />
        <Button
            android:id="@+id/btn_second"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="๋‘๋ฒˆ์งธ"
            />
    </LinearLayout>

	<!--ํ”„๋ž˜๊ทธ๋จผํŠธ๋ฅผ ์ถ”๊ฐ€ํ•  container -->
    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:background="@color/teal_700"
        />

</LinearLayout>

 

first_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="์ฒซ๋ฒˆ์งธ ํ”„๋ž˜๊ทธ๋จผํŠธ"
        android:textSize="28sp"
        android:textStyle="bold"/>

</RelativeLayout>

 

second_fragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="๋‘๋ฒˆ์งธ ํ”„๋ž˜๊ทธ๋จผํŠธ"
        android:textSize="28sp"
        android:textStyle="bold"/>

</RelativeLayout>

 

MainActivty.kt

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.fragment.app.Fragment
import com.lee.fragmentreplayexample.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding : ActivityMainBinding
    private lateinit var firstFragment : Fragment
    private lateinit var secondFragment : Fragment
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater).also {
            setContentView(it.root)
        }
        if(savedInstanceState == null){ // ์ฒ˜์Œ ์ƒ์„ฑ๋˜๋Š” ๊ฒฝ์šฐ Fragment๋ฅผ ์ƒ์„ฑํ•ด์ค€๋‹ค.
            firstFragment = FirstFragment()
            secondFragment = SecondFragment()
        }

        with(binding){
            btnFirst.setOnClickListener {
                val ft = supportFragmentManager.beginTransaction()
                // FragmentManager๋ฅผ ํ†ตํ•ด ๊ด€๋ฆฌ๋˜๋ฉฐ beginTransaction์„ ํ†ตํ•ด Fragment์˜ ์ƒ๋ช…์ฃผ๊ธฐ๋Š” ์‹œ์ž‘๋œ๋‹ค.
                ft.replace(R.id.container , firstFragment).commit()
            }

            btnSecond.setOnClickListener {
                val ft = supportFragmentManager.beginTransaction()
                ft.replace(R.id.container , secondFragment).commit()
            }
        }
    }
}

 

FirstFragment.kt

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

 

SecondFragment.kt

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

 

 


์š”์ฆ˜ ์•ˆ๋“œ๋กœ์ด๋“œ์—์„œ ํ”„๋ž˜๊ทธ๋จผํŠธ๋Š” ์—†์–ด์„œ๋Š” ์•ˆ ๋  ์กด์žฌ์ด๊ธฐ์—

๋” ๊ณต๋ถ€ํ•˜์—ฌ ์ ์žฌ์ ์†Œ์— ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋„๋ก ๋ฐœ์ „ํ•ด์•ผ๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์ด์ƒ์œผ๋กœ ํ”„๋ž˜๊ทธ๋จผํŠธ์— ๋Œ€ํ•œ ํฌ์ŠคํŒ…์„ ๋งˆ์น˜๊ฒ ์Šต๋‹ˆ๋‹ค.

 

์˜ค๋Š˜๋„ ์ฆ์ฝ”ํ•˜์„ธ์š” :)