์๋ ํ์ธ์ ๐
์ค๋์ ์๋๋ก์ด๋ ํ๋๊ทธ๋จผํธ์ ๋ํ์ฌ
๊ณต๋ถํ ๋ด์ฉ์ ํฌ์คํ ํด๋ณด๋ ค ํฉ๋๋ค.

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)
}
}
์์ฆ ์๋๋ก์ด๋์์ ํ๋๊ทธ๋จผํธ๋ ์์ด์๋ ์ ๋ ์กด์ฌ์ด๊ธฐ์
๋ ๊ณต๋ถํ์ฌ ์ ์ฌ์ ์์ ์ฌ์ฉํ ์ ์๋๋ก ๋ฐ์ ํด์ผ๊ฒ ์ต๋๋ค.
์ด์์ผ๋ก ํ๋๊ทธ๋จผํธ์ ๋ํ ํฌ์คํ ์ ๋ง์น๊ฒ ์ต๋๋ค.
์ค๋๋ ์ฆ์ฝํ์ธ์ :)