LifeCycleActivity.kt
package com.example.activityandfragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
class LifeCycleActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_life_cycle)
Log.d("mylifecycle", "onCreate() 수행")
}
override fun onStart() {
super.onStart()
Log.d("mylifecycle", "onStart() 수행")
}
override fun onRestart() {
super.onRestart()
Log.d("mylifecycle", "onRestart() 수행")
}
override fun onResume() {
super.onResume()
Log.d("mylifecycle", "onResume() 수행")
}
override fun onPause() {
super.onPause()
Log.d("mylifecycle", "onPause() 수행")
}
override fun onStop() {
super.onStop()
Log.d("mylifecycle", "onStop() 수행")
}
override fun onDestroy() {
super.onDestroy()
Log.d("mylifecycle", "onDestroy() 수행")
}
}
activity_main.xml
<?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/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go to SubActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button_exit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="종료"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button"
app:layout_constraintVertical_bias="0.204" />
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MainActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.046" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_sub.xml
<?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=".SubActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SubActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.078" />
<Button
android:id="@+id/buttonClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to MainActivity"
app:layout_constraintBottom_toTopOf="@id/buttonExit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView" />
<Button
android:id="@+id/buttonExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="종료"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt
<?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=".SubActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SubActivity"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.078" />
<Button
android:id="@+id/buttonClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="go to MainActivity"
app:layout_constraintBottom_toTopOf="@id/buttonExit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView" />
<Button
android:id="@+id/buttonExit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="종료"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
SubActivity.kt
package com.example.activityandfragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.Toast
class SubActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_sub)
settingButton()
Log.d("SubActivity:mylifecycle", "onCreate() 수행")
}
private fun settingButton() {
val buttonClose = findViewById<Button>(R.id.buttonClose)
buttonClose.setOnClickListener {
finish()
}
val buttonExit = findViewById<Button>(R.id.buttonExit)
buttonExit.setOnClickListener {
Toast.makeText(applicationContext, "종료합니다.", Toast.LENGTH_SHORT).show()
finishAffinity()
}
}
override fun onStart() {
super.onStart()
Log.d("SubActivity:mylifecycle", "onStart() 수행")
}
override fun onRestart() {
super.onRestart()
Log.d("SubActivity:mylifecycle", "onRestart() 수행")
}
override fun onResume() {
super.onResume()
Log.d("SubActivity:mylifecycle", "onResume() 수행")
}
override fun onPause() {
super.onPause()
Log.d("SubActivity:mylifecycle", "onPause() 수행")
}
override fun onStop() {
super.onStop()
Log.d("SubActivity:mylifecycle", "onStop() 수행")
}
override fun onDestroy() {
super.onDestroy()
Log.d("SubActivity:mylifecycle", "onDestroy() 수행")
}
}
activity_two_color.xml
<?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=".TwoColorActivity">
<FrameLayout
android:id="@+id/fragmentFrame"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#FF9800"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/button_yellow_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="YELLOW Fragment"
app:layout_constraintBottom_toTopOf="@id/button_blue_fragment"
android:layout_margin="30dp"
/>
<Button
android:id="@+id/button_red_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RED Fragment"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="30dp"
/>
<Button
android:id="@+id/button_blue_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BLUE Fragment"
android:layout_margin="30dp"
app:layout_constraintBottom_toTopOf="@id/button_red_fragment"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
RedFragment.kt
package com.example.activityandfragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class RedFragment : Fragment(){
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_red, container, false)
}
}
BlueGragment.kt
package com.example.activityandfragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class BlueFragment : Fragment() {
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_blue, container, false)
}
}
YellowFragment.kt
package com.example.activityandfragment
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
class YellowFragment : Fragment(){
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_yellow, container, false)
}
}
TwoColorActivity.kt
package com.example.activityandfragment
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class TwoColorActivity : AppCompatActivity() {
lateinit var redButton:Button
lateinit var blueButton:Button
lateinit var yellowButton:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_two_color)
settingButtons()
yellowButton.performClick()
}
private fun settingButtons() {
redButton = findViewById<Button>(R.id.button_red_fragment)
blueButton = findViewById<Button>(R.id.button_blue_fragment)
yellowButton = findViewById<Button>(R.id.button_yellow_fragment)
redButton.setOnClickListener {
updateButtonStates(redButton)
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentFrame, RedFragment())
fragmentTransaction.commit()
}
blueButton.setOnClickListener {
updateButtonStates(blueButton)
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentFrame, BlueFragment())
fragmentTransaction.commit()
}
yellowButton.setOnClickListener {
updateButtonStates(yellowButton)
val fragmentTransaction = supportFragmentManager.beginTransaction()
fragmentTransaction.replace(R.id.fragmentFrame, YellowFragment())
fragmentTransaction.commit()
}
}
private fun updateButtonStates(activeButton: Button) {
blueButton.isEnabled = true
redButton.isEnabled = true
yellowButton.isEnabled = true
activeButton.isEnabled = false
}
}
activity_life_cycle.xml
<?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=".LifeCycleActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LifeCycle example"
android:textColor="#E91E63"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
activity_two_color.xml
<?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=".TwoColorActivity">
<FrameLayout
android:id="@+id/fragmentFrame"
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#FF9800"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="@+id/button_yellow_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="YELLOW Fragment"
app:layout_constraintBottom_toTopOf="@id/button_blue_fragment"
android:layout_margin="30dp"
/>
<Button
android:id="@+id/button_red_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RED Fragment"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_margin="30dp"
/>
<Button
android:id="@+id/button_blue_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BLUE Fragment"
android:layout_margin="30dp"
app:layout_constraintBottom_toTopOf="@id/button_red_fragment"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0000FF">
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_red.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
>
</androidx.constraintlayout.widget.ConstraintLayout>
fragment_yellow.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFEB3B"
>
</androidx.constraintlayout.widget.ConstraintLayout>
activity_main.xml
<?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"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:text="button1"
android:layout_width="150dp"
android:layout_height="400dp"/>
</androidx.constraintlayout.widget.ConstraintLayout>