#1 ์ด์ ๊ธ
[Android] Activity์ ์๋ช ์ฃผ๊ธฐ
#1 Android Application์ ์์ (AndroidManifext.xml) ... ์ฐ๋ฆฌ๊ฐ ์ฑ ์์ด์ฝ์ ๋๋ฅด๋ฉด, ์๋๋ก์ด๋ ์์คํ ์ ํด๋น ์ฑ์ Launcher Activity๋ฅผ ์คํ์ํจ๋ค. AndroidManifext.xml์ ๋ฑ๋ก๋ ์กํฐ๋นํฐ ์ค์์ ํ๊ทธ๋ฅผ ๊ฐ์ง๊ณ
kenel.tistory.com
ํ๋๊ทธ๋จผํธ๋ ์กํฐ๋นํฐ์ ๋น์ทํ์ง๋ง, ๊ฐ์ฅ ์ฃผ์ํ ์ฐจ์ด์ ์ ํ๋ก ์ค ์ ์์ผ๋ฉฐ ๋ฐ๋์ ์กํฐ๋นํฐ๋ ๋ ๋ค๋ฅธ ํ๋๊ทธ๋จผํธ์ ์ข
์๋์ด ๋์ํ๋ค๋ ๊ฒ์ด๋ค. ํ๋๊ทธ๋จผํธ๋ ์กํฐ๋นํฐ์ฒ๋ผ ์๋ช
์ฃผ๊ธฐ๋ฅผ ๊ฐ์ง๋ฉฐ, ์ข
์๋ ์กํฐ๋นํฐ๋ ๋ ๋ค๋ฅธ ํ๋๊ทธ๋จผํธ์ ์๋ช
์ฃผ๊ธฐ์ ์ํฅ์ ๋ฐ๊ธฐ๋ ํ๋ค. ์ด์ ๊ธ์ ์ด์ด ์ด๋ฒ์๋ Fragment์ ์๋ช
์ฃผ๊ธฐ๋ฅผ ์ดํด๋ณธ๋ค.
#2 Fragment์ ์๋ช ์ฃผ๊ธฐ ํ๋ฆ๋

๊ฐ ์๋ช
์ฃผ๊ธฐ์ ํด๋นํ๋ ์ฝ๋ฐฑ ํจ์๋ค์ ์ญํ ์ Activity์์์ ๊ฑฐ์ ๋น์ทํ๋ค. ์ค๋ช
๋ณด๋ค๋ ์ค์ ์ฑ์ ํตํด ๊ฐ์ ์ก๋ ๊ฒ์ด ์ข๋ค. ์ด์ ๊ธ์์์ ๋ง์ฐฌ๊ฐ์ง๋ก, ์๋ช
์ฃผ๊ธฐ์ ๋ฐ๋ฅธ ์ฝ๋ฐฑ ํจ์๋ค์ Log๋ฅผ ๋ฌ์ ์๋ช
์ฃผ๊ธฐ๋ฅผ ์ดํด๋ณผ ์ ์๋ ์ฑ์ ๋ง๋ค์ด๋ณด์๋ค.
#3 ํ๋๊ทธ๋จผํธ ์๋ช ์ฃผ๊ธฐ ์ดํด๋ณด๊ธฐ์ฉ ์ฑ
#3-1 ๊ฐ์

์กํฐ๋นํฐ 2๊ฐ(MainActivity, SecondActivity)๋ฅผ ์ฌ์ฉํ๋ค. ๊ฐ๊ฐ์ ์กํฐ๋นํฐ๋ ๋ฐํ
๋ด๋น๊ฒ์ด์
๋ฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ๋ฐํ
๋ด๋น๊ฒ์ด์
๋ฐ์ ์์ดํ
์ด 3๊ฐ์ฉ์ด๋ฏ๋ก, ํ๋๊ทธ๋จผํธ๋ 3๊ฐ์ฉ ๊ฐ์ง๊ณ ์๋ค. ํ๋๊ทธ๋จผํธ ๋ด๋ถ์ ๋ฒํผ์ ๋๋ฌ ์กํฐ๋นํฐ ๊ฐ ์ด๋๋ ๊ฐ๋ฅํ๋ค.
#3-2 ์ดํด๋ณด๊ธฐ์ฉ ์ฑ์ ๋ง๋ค๊ธฐ ์ํ ํ ํ๋ฆฟ

[File] โ [New] โ [New Project...] โ [Bottom Navigation Views Activity]์ ํ
ํ๋ฆฟ์ผ๋ก ํ๋ก์ ํธ๋ฅผ ๋ง๋ค๊ณ , ์์ ํ๋ค.
#3-3 MainActivity.kt
// package com.example.fragmentlifecyclelogger
import android.content.Intent
import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.NavController
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import com.example.fragmentlifecyclelogger.databinding.ActivityMainBinding
import com.google.android.material.bottomnavigation.BottomNavigationView
class MainActivity : AppCompatActivity(), OnBottomNavUiChangeListener {
private lateinit var binding: ActivityMainBinding
private lateinit var navView: BottomNavigationView
private lateinit var navController: NavController
override fun onCreate(savedInstanceState: Bundle?) {
Log.i("interfacer_han", "MainActivity.onCreate()")
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
navView = binding.navView
navView.setOnItemSelectedListener { item ->
Log.i("interfacer_han", "(MainActivity) ${item.title} clicked")
navController.navigate(item.itemId)
true
}
navController = findNavController(R.id.nav_host_fragment_activity_main)
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
val appBarConfiguration = AppBarConfiguration(
setOf(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications
)
)
setupActionBarWithNavController(navController, appBarConfiguration)
//navView.setupWithNavController(navController) // ์์ ์๋ ์ปค์คํ
๋ฆฌ์ค๋๋ก ๋์ฒด
navigateFragment()
}
override fun onStart() {
Log.i("interfacer_han", "MainActivity.onStart()")
super.onStart()
}
override fun onNewIntent(newIntent: Intent?) {
super.onNewIntent(newIntent)
intent = newIntent
}
private fun navigateFragment() {
Log.i(
"interfacer_han",
"(MainActivity) intent.getStringExtra(\"fragment_info_key\"): ${intent.getStringExtra("fragment_info_key")}"
)
/* ์์ ์๋ onNewIntent()๊ฐ ์์ด์ผ,
* ์คํ์ ์๋ค๊ฐ ์ฌํธ์ถ(onResume())๋ Activity์ intent.getStringExtra("fragment_info_key")์ ๊ฐ์ด
* ํ๋๋ก ๊ณ ์ ๋์ง ์๋๋ค.
*/
when (intent.getStringExtra("fragment_info_key")) {
null -> {/* ์๋ฌด ๊ฒ๋ ์ํจ. ๊ธฐ๋ณธ๊ฐ์ผ๋ก ์ค์ ๋ ํ๋๊ทธ๋จผํธ ํธ์ถ๋จ */}
"HomeFragment" -> {
navController.navigate(R.id.navigation_home)
}
"DashboardFragment" -> {
navController.navigate(R.id.navigation_dashboard)
}
"NotificationsFragment" -> {
navController.navigate(R.id.navigation_notifications)
}
}
}
override fun onResume() {
Log.i("interfacer_han", "MainActivity.onResume()")
super.onResume()
navigateFragment()
}
override fun onPause() {
Log.i("interfacer_han", "MainActivity.onPause()")
super.onPause()
}
override fun onStop() {
Log.i("interfacer_han", "MainActivity.onStop()")
super.onStop()
}
override fun onRestart() {
Log.i("interfacer_han", "MainActivity.onRestart()")
super.onRestart()
}
override fun onDestroy() {
Log.i("interfacer_han", "MainActivity.onDestroy()")
super.onDestroy()
}
override fun changeBottomNavUi(selectedItemId: Int) {
val menuItem = navView.menu.findItem(selectedItemId)
if(!menuItem.isChecked) {
menuItem.isChecked = true
}
}
}
SecondActivity์ ์ฝ๋๋ ๊ฑฐ์ ๊ฐ๋ค. ์ ์ฒด ์์ค์ฝ๋๋ #5์ ์๋ค.
#3-4 HomeFragment.kt
// package com.example.fragmentlifecyclelogger.ui.home
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.fragment.app.Fragment
import com.example.fragmentlifecyclelogger.OnBottomNavUiChangeListener
import com.example.fragmentlifecyclelogger.R
import com.example.fragmentlifecyclelogger.SecondActivity
import com.example.fragmentlifecyclelogger.databinding.FragmentHomeBinding
class HomeFragment : Fragment() {
private var _binding: FragmentHomeBinding? = null
// This property is only valid between onCreateView and
// onDestroyView.
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onCreateView()")
_binding = FragmentHomeBinding.inflate(inflater, container, false)
val root: View = binding.root
val button: Button = binding.buttonHome
button.setOnClickListener {
val intent = Intent(activity, SecondActivity::class.java)
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT) // ์ด ํ๋๊ทธ๊ฐ ์์ผ๋ฉด, Activity๋ฅผ ์ ํํ ๋๋ง๋ค Activity๊ฐ onCreate()๋จ
intent.putExtra("fragment_info_key", "Home2Fragment")
startActivity(intent)
}
return root
}
override fun onDestroyView() {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onDestoryView()")
super.onDestroyView()
_binding = null
}
override fun onCreate(savedInstanceState: Bundle?) {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onCreate()")
super.onCreate(savedInstanceState)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onViewCreated()")
super.onViewCreated(view, savedInstanceState)
}
override fun onViewStateRestored(savedInstanceState: Bundle?) {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onViewStateRestored()")
super.onViewStateRestored(savedInstanceState)
}
override fun onStart() {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onStart()")
super.onStart()
val nowActivity = activity
if(nowActivity is OnBottomNavUiChangeListener) {
nowActivity.changeBottomNavUi(R.id.navigation_home)
}
}
override fun onResume() {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onResume()")
super.onResume()
}
override fun onPause() {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onPause()")
super.onPause()
}
override fun onStop() {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onStop()")
super.onStop()
}
override fun onSaveInstanceState(outState: Bundle) {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onSaveInstanceState()")
super.onSaveInstanceState(outState)
}
override fun onDestroy() {
Log.i("interfacer_han", "(MainActivity) HomeFragment.onDestroy()")
super.onDestroy()
}
}
๋๋จธ์ง 5๊ฐ์ ํ๋๊ทธ๋จผํธ๋ค์ ์ฝ๋๋ ๊ฑฐ์ ๊ฐ๋ค. ์ ์ฒด ์์ค์ฝ๋๋ #5์ ์๋ค.
#4 ๋ก๊ทธ ํ์ธ

์กํฐ๋นํฐ์ ์๋ช
์ฃผ๊ธฐ๋ ๋นจ๊ฐ์ ๊ธ์จ๋ก ํ์ํ๋ค.
1. ์์ ํ๋ฉด (MainActivity)
MainActivity.onCreate()
HomeFragment.onCreate()
HomeFragment.onCreateView()
HomeFragment.onViewCreated()
MainActivity.onStart()
HomeFragment.onViewStateRestored()
HomeFragment.onStart()
MainActivity.onResume()
HomeFragment.onResume()
2. MainActivity ๋ด์์ Dashboard ํ๋๊ทธ๋จผํธ๋ก ์ ํ
HomeFragment.onPause()
HomeFragment.onStop()
DashboardFragment.onCreate()
DashboardFragment.onCreateView()
DashboardFragment.onViewCreated()
DashboardFragment.onViewStateRestored()
DashboardFragment.onStart()
HomeFragment.onDestoryView()
DashboardFragment.onResume()
3. SecondActivity์ Dashboard2 ํ๋๊ทธ๋จผํธ๋ก ์ด๋
MainActivity.onPause()
DashboardFragment.onPause()
SecondActivity.onCreate()
Home2Fragment.onCreate()
Home2Fragment.onCreateView()
Home2Fragment.onViewCreated()
SecondActivity.onStart()
Home2Fragment.onViewStateRestored()
Dashboard2Fragment.onCreate()
Dashboard2Fragment.onCreateView()
Dashboard2Fragment.onViewCreated()
Dashboard2Fragment.onViewStateRestored()
Home2Fragment.onDestoryView()
Dashboard2Fragment.onStart()
SecondActivity.onResume()
Dashboard2Fragment.onStop()
Dashboard2Fragment.onCreate()
Dashboard2Fragment.onCreateView()
Dashboard2Fragment.onViewCreated()
Dashboard2Fragment.onViewStateRestored()
Dashboard2Fragment.onStart()
Dashboard2Fragment.onDestoryView()
Dashboard2Fragment.onResume()
MainActivity.onStop()
DashboardFragment.onStop()
HomeFragment.onSaveInstanceState()
DashboardFragment.onSaveInstanceState()
4. SecondActivity ๋ด์์ Notification2 ํ๋๊ทธ๋จผํธ๋ก ์ ํ
Dashboard2Fragment.onPause()
Dashboard2Fragment.onStop()
Notifications2Fragment.onCreate()
Notifications2Fragment.onCreateView()
Notifications2Fragment.onViewCreated()
Notifications2Fragment.onViewStateRestored()
Notifications2Fragment.onStart()
Dashboard2Fragment.onDestoryView()
Notifications2Fragment.onResume()
5. MainActivity์ Notification ํ๋๊ทธ๋จผํธ๋ก ์ด๋
SecondActivity.onPause()
Notifications2Fragment.onPause()
MainActivity.onRestart()
MainActivity.onStart()
DashboardFragment.onStart()
MainActivity.onResume()
DashboardFragment.onStop()
NotificationsFragment.onCreate()
NotificationsFragment.onCreateView()
NotificationsFragment.onViewCreated()
NotificationsFragment.onViewStateRestored()
NotificationsFragment.onStart()
DashboardFragment.onDestoryView()
NotificationsFragment.onResume()
SecondActivity.onStop()
Notifications2Fragment.onStop()
Dashboard2Fragment.onSaveInstanceState()
Notifications2Fragment.onSaveInstanceState()
Home2Fragment.onSaveInstanceState()
Dashboard2Fragment.onSaveInstanceState()
6. MainActivity ๋ด์์ Dashboard ํ๋๊ทธ๋จผํธ๋ก ์ ํ
NotificationsFragment.onPause()
NotificationsFragment.onStop()
DashboardFragment.onCreate()
DashboardFragment.onCreateView()
DashboardFragment.onViewCreated()
DashboardFragment.onViewStateRestored()
DashboardFragment.onStart()
NotificationsFragment.onDestoryView()
DashboardFragment.onResume()
#5 ์์ฝ
ํ๋๊ทธ๋จผํธ๋ ์์ Activity์ฒ๋ผ ๋์ํ๋ค.
#6 ์์ฑ๋ ์ฑ
https://github.com/Kanmanemone/android-practice/tree/master/lifecycle/FragmentLifecycleLogger
'๊นจ์ ๊ฐ๋ ๐ > Android' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Android] Navigation - ๊ธฐ์ด (0) | 2024.01.26 |
---|---|
[Android] Navigation - ํ๊ฒฝ ์ค์ (0) | 2024.01.25 |
[Android] Activity์ ์๋ช ์ฃผ๊ธฐ (0) | 2024.01.23 |
[Android] MVVM ๊ตฌ์กฐ ํ๋์ ๋ณด๊ธฐ (0) | 2024.01.22 |
[Android] LiveData - ์๋ฐฉํฅ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ์ 3๊ฐ์ง ๋ฐฉ๋ฒ (0) | 2024.01.20 |