스터디/안드로이드
[Android/Kotlin] fragment특징과 생명주기(lifecycle) + 예제
혜유우
2023. 1. 19. 00:34
fragment
-부분화면이면서 액티비티처럼 독립적으로 동작함
-액티비티 위에 올라와야 정상적인 프래그먼트로 동작함
-Activity처럼 life cycle이 존재
-프래그먼트 매니저에 의해 관리됨
프래그먼트의 생명주기(Lifecycle)
1. 액티비티에 프래그먼트 추가
onAttach() -> onCreate() -> onCreateView() -> onActivityCreated() -> onStart() -> onResume() -> 프래그먼트 활성화
2. 액티비티에서 프래그먼트 제거
프래그먼트 활성화 -> onPause() -> onStop() -> onDestroyView() -> onDestroy() -> onDetach()
+예제
로그인/메뉴 화면을 fragment로 만들어보자!
package com.mobile.fragment
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.fragment_menu.view.*
class LoginFragment : Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val rootView = inflater.inflate(R.layout.fragment_login, container, false)
rootView.nextButton.setOnClickListener{
val mainActivity = activity as MainActivity
mainActivity.onFragmentChanged(1)
}
return rootView
}
}
package com.mobile.fragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
var loginFragment:LoginFragment = LoginFragment()
var menuFragment:MenuFragment = MenuFragment()
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
showLoginButton.setOnClickListener{
//supportFragmentManager.beginTransaction().replace(R.id.container, LoginFragment()).commit()
// with(supportFragmentManager.beginTransaction()){
// replace(R.id.container, loginFragment)
// }.commit()
onFragmentChanged(0)
}
showMenuButton.setOnClickListener{
//supportFragmentManager.beginTransaction().replace(R.id.container, MenuFragment()).commit()
// with(supportFragmentManager.beginTransaction()){
// replace(R.id.container, menuFragment)
// }.commit()
onFragmentChanged(1)
}
}
fun onFragmentChanged(index:Int){
when(index){
0 -> {
with(supportFragmentManager.beginTransaction()){
replace(R.id.container, loginFragment)
}.commit()
}
1 -> {
with(supportFragmentManager.beginTransaction()){
replace(R.id.container, menuFragment)
}.commit()
}
}
}
}
package com.mobile.fragment
import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
class MenuFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_menu, container, false)
}
}
<?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">
<Button
android:id="@+id/showLoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginTop="10dp"
android:text="로그인"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:id="@+id/container"
android:layout_width="409dp"
android:layout_height="671dp"
android:layout_marginStart="1dp"
android:layout_marginTop="5dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/showLoginButton">
<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragmentContainerView2"
android:name="com.mobile.fragment.LoginFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
<Button
android:id="@+id/showMenuButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="28dp"
android:layout_marginTop="10dp"
android:text="메뉴"
app:layout_constraintStart_toEndOf="@+id/showLoginButton"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FBD5D5"
android:orientation="vertical"
tools:context=".LoginFragment">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="로그인 화면"
android:textSize="40sp" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/nextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="다음으로" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/loginFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#DCECF3"
android:orientation="vertical"
tools:context=".LoginFragment">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="메뉴 화면"
android:textSize="40sp" />
<EditText
android:id="@+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="@+id/nextButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>