โ ์กํฐ๋นํฐ ์๋ช ์ฃผ๊ธฐ ๋ฉ์๋ ์ค๋ช
- onCreate() : ์กํฐ๋นํฐ๊ฐ ์ฒ์ ์์ฑ๋ ๋ ํธ์ถ, UI ๊ตฌ์ฑ, ๋ณ์ ์ด๊ธฐํ ๋ฆฌ์ค๋ ๋ฑ๋ก ๋ฑ์ ์ํ
- onResume() : ์กํฐ๋นํฐ๊ฐ ํ๋ฉด์ ๋ณด์ด๊ณ , ์ฌ์ฉ์์ ์ํธ์์ฉ ๊ฐ๋ฅํด์ง ๋ ํธ์ถ, ์ฑ์ด ํฌ๊ทธ๋ผ์ด๋๋ก ๋ณต๊ทํ ๋๋ ํธ์ถ๋จ
- onPause() : ๋ค๋ฅธ ์กํฐ๋นํฐ๊ฐ ์์ ๋ํ๋ ๋ ์ ์ ์ค๋จ๋๋ ์์ ์ ํธ์ถ, ํ๋ฉด์ ๋ณด์ด์ง๋ง ์ํธ์์ฉ์ ์ ํ๋ ์ ์์
- onDestory() : ์กํฐ๋นํฐ๊ฐ ์์ ๋ฆฌ ์ข ๋ฃ๋๊ธฐ ์ง์ ์ ํธ์ถ, ๋ฆฌ์์ค ํด์ , ๋ฐฑ์ ์ ์ฅ ๋ฑ ์ ๋ฆฌ ์์ ์ํ
- onBackPressed() : ์ฌ์ฉ์๊ฐ ๋ค๋ก ๊ฐ๊ธฐ ๋ฒํผ์ ๋๋ ์ ๋ ํธ์ถ
๐ ์๋ช ์ฃผ๊ธฐ ํ๋ฆ ์์
- ์ฑ ์คํ → onCreate() → onStart() → onResume()
- ํ๋ฉด ์ ์ ๋ฒ์ด๋จ → onPause() → onStop()
- ๋ค์ ๋์์ด → onRestart() → onStart() → onResume()
- ์ฑ ์ข ๋ฃ → onPause() → onStop() → onDestroy()
โ ForeGround vs BackGround
- ํฌ๊ทธ๋ผ์ด๋ : ์ฑ์ด ์ผ์ ธ ์๋ ์ํ, ์ฆ ์ ์ ๊ฐ ์ฑ์ ์ฌ์ฉํ๊ณ ์๋ ์ํ
- ๋ฐฑ๊ทธ๋ผ์ด๋ : ์ฑ์ด ๊บผ์ ธ ์๊ฑฐ๋, ํ ๋ฒํผ์ ๋๋ฌ ๋น ์ ธ๋์จ ์ํ
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {}
override fun onActivityStarted(activity: Activity) {}
override fun onActivityResumed(activity: Activity) {}
override fun onActivityPaused(activity: Activity) {}
override fun onActivityStopped(activity: Activity) {}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle){}
override fun onActivityDestroyed(activity: Activity){}
โ ์ง์ /์ดํ ํ๋จ ๋ก์ง
private var activityTracking = 0 //ํ์ฌ ์คํ ์ค์ธ ์กํฐ๋นํฐ์ ์๋ฅผ ์ถ์
private var configCheck = false //๋ณ๊ฒฝ ์ฌ๋ถ ํ์ธ
companion object{
var isForeground = true //์ํ ์ค์์น
var doOpenWhenStop = true
}//end of object
override fun onActivityStarted(activity: Activity) {
if(++activityTracking==1&&!configCheck){
isForeground=true
doOpenWhenStop=true
}//end of if
}//end of Started
override fun onActivityStopped(activity: Activity) {
configCheck = activity.isChangingConfigurations
if (--activityTracking==0&&!configCheck){
isForeground = false
}//end of if
}//end of Stopped
- ++ activityTracking : activityTracking ๊ฐ์ 1์ฆ๊ฐ ์ํค๊ณ ๊ทธ ๊ฐ์ ํ๊ฐ
- ==1 : ํ์ฌ ์คํ ์ค์ธ ์กํฐ๋นํฐ๊ฐ 1๊ฐ ๋ฟ์ด๋ผ๋ ์๋ฏธ, ์ฆ ํฌ๊ทธ๋ผ์ด๋๋ก ์ง์ ํ ์์
- ! configCheck : ๊ตฌ์ฑ ๋ณ๊ฒฝ (configuration change) ์ค์ธ์ง ํ๋จ
- ๊ตฌ์ฑ ๋ณ๊ฒฝ : ์ฑ ๋์คํ๋ ์ด์ค ํฌ๊ธฐ, ํ๋ฉด ๋ฐฉํฅ, ๊ธ๊ผด ํฌ๊ธฐ ๋ฐ ๋๊ป, ์ธ์ด ๋ฑ ์ฌ์ฉ์ ์ํธ์์ฉ์ผ๋ก ์ธํด ๋ฐ์
- ex) ํ๋ฉด ํ์ ์ ์กํฐ๋นํฐ๊ฐ destroy → recreate๋จ ์ด๋๋ onActivityStopped() → onActivityStarted() ํธ์ถ๋์ง๋ง, ๊ตฌ์ฑ ๋ณ๊ฒฝ์ด๋ฏ๋ก configCheck == true → ๋ฌด์ํจ
- ๊ตฌ์ฑ ๋ณ๊ฒฝ : ์ฑ ๋์คํ๋ ์ด์ค ํฌ๊ธฐ, ํ๋ฉด ๋ฐฉํฅ, ๊ธ๊ผด ํฌ๊ธฐ ๋ฐ ๋๊ป, ์ธ์ด ๋ฑ ์ฌ์ฉ์ ์ํธ์์ฉ์ผ๋ก ์ธํด ๋ฐ์
- ++ activityTracking==1 && ! configCheck , ๊ตฌ์ฑ ๋ณ๊ฒฝ์ ์ํ ์ฌ์์์ ์ ์ธ ์ฑ์ด ์์ ํ ๋ฐฑ๊ทธ๋ผ์ด๋ ์ํ์์ ์๋ก ์์๋ ์ํ ์์ ํ True(ํฌ๊ทธ๋ผ์ด๋ ์ํ)
์ํฉ | activityTracking | ์กฐ๊ฑด ๋ง์กฑ ์ฌ๋ถ |
์ฑ์ด ์ฒ์ ์ด๋ฆผ | 0 → ++ → 1 | โ true |
๋ ๋ฒ์งธ ์กํฐ๋นํฐ ์ด๋ฆผ | 1 → ++ → 2 | โ false |
ํ๋ฉด ํ์ ์ผ๋ก ์ฌ์์ | 1 → 0 → ++ → 1 | โ (๊ตฌ์ฑ ๋ณ๊ฒฝ ์ค์ด๋ฏ๋ก configCheck== true) |
์์ | ์ด๋ฒคํธ | activityTracking | ์ํ |
์ฑ ์ฒ์ ์คํ | A ์์ | 0 → 1 | ํฌ๊ทธ๋ผ์ด๋ ์ง์ |
๋ค๋ฅธ ์กํฐ๋นํฐ B ์คํ | B ์์ | 1 → 2 | ์ ์ง |
B ์ข ๋ฃ | B ์ค์ง | 2 → 1 | ์ ์ง |
A ์ข ๋ฃ | A ์ค์ง | 1 → 0 | ๋ฐฑ๊ทธ๋ผ์ด๋ ์ง์ |
override fun onActivityStopped(activity: Activity) {
configCheck = activity.isChangingConfigurations
if (--activityTracking==0&&!configCheck){
isForeground = false
if(doOpenWhenStop){
Handler(Looper.myLooper()!!).postDelayed({
if(!isForeground){
var actManager = getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager
var appTasks = actManager.appTasks
appTasks.firstOrNull()?.moveToFront()
}//end of if
},10_000L)//end of handler
}//end of if
}//end of if
}//end of Stopped
- --activityTracking==0&&!configCheck : ๋ฐฑ๊ทธ๋ผ์ด๋ ์ํ ์ง์
- Handler : ๋ฉ์ธ์ง๋ ์์ ์ ํน์ ์ค๋ ๋์ ๋ฉ์์ง ํ์ ๋ฃ์ด ์์ ์ ์์ฝ/์ง์ฐ ์คํ, ์ง์ ๋ Looper(๋ฉ์์ง ๋ฃจํ)๋ฅผ ๊ฐ์ง ์ค๋ ๋์์ ์์ ์ ์คํ, ์ฆ ํด๋น Looper์ ์ฐ๊ฒฐ๋ ์ค๋ ๋์์ ์์ ์ ์ฒ๋ฆฌ ํ๋ ์ฝ๋
- Looper.myLooper() : ํ์ฌ ์คํ์ค์ธ ์ค๋ ๋์ Looper ๊ฐ์ฒด๋ฅผ ๋ฐํ, !!(Null์ด ์๋์ ํ์ ๊ฐ์ ์ฌ์ฉ)
- .postDelayed(Runnable, delayMillis) : Runnable(์คํํ ์ฝ๋), delayMillis(๋๊ธฐ์๊ฐ)
- 10์ด ํ isForeground ์ํ ํ์ธ ํ ture ์ผ ๋ ์ฑ์ ๋ค์ ํฌ๊ทธ๋ผ์ด๋๋ก ๋์ด์ฌ๋ฆฌ๋ ์์
์คํ ์ง์
- getSystemService(String name) ์๋๋ก์ด๋ ์์คํ ์๋น์ค์ ์ ๊ทผํ ์ ์๊ฒ ํด์ค
- Context.ACTIVITY_SERVICE : Activity Manager๋ฅผ ์์ฒญํ๋ ์์ ํค
- ์ฌ์ฉ ๊ฐ๋ฅํ ์์ ๋๋ฉ์ธ - https://developer.android.com/reference/android/content/Context
- .appTasks : ์ฑ์์ ์คํ ์ค์ธ ์์ ๋ชฉ๋ก์ ๋ฐํ
- .firstOrNull?().moveToFront() : ์ฑ์ ๊ฐ์ฅ ์ฒซ๋ฒ ์งธ ์์ ์ ๊ฐ์ ธ์จ ํ ํด๋น ์์ ์ ๋งจ ์์ผ๋ก ๋ณด์ฌ์ฃผ๋ ์ญํ
'๐๐ฏ๐ฅ๐ณ๐ฐ๐ช๐ฅ๐๐ต๐ถ๐ฅ๐ช๐ฐ-๐๐ฐ๐ต๐ญ๐ช๐ฏ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
View Binding (0) | 2025.06.07 |
---|