반응형
Application
설명
- 애플리케이션 컴포넌트 사이에서 공통 멤버를 사용할 수 있게 해주는 클래스
- 공통 초기화 로직이나 공통 데이터 등을 관리하는 목적으로 사용
코드
AppApplicationclass AppApplication : Application() {
companion object {
var message = "Hello"
}
override fun onCreate() {
super.onCreate()
Toast.makeText(this, "App Start", Toast.LENGTH_SHORT).show()
registerActivityLifecycleCallbacks(AppActivityLifecycleCallbacks()) // 모든 액티비티 Lifecycle 감시
}
inner class AppActivityLifecycleCallbacks : ActivityLifecycleCallbacks {
override fun onActivityPaused(activity: Activity) {
Log.d("TEST", "onActivityPaused - ${activity.javaClass.simpleName}")
}
override fun onActivityStarted(activity: Activity) {
Log.d("TEST", "onActivityStarted - ${activity.javaClass.simpleName}")
}
override fun onActivityDestroyed(activity: Activity) {
Log.d("TEST", "onActivityDestroyed - ${activity.javaClass.simpleName}")
}
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) {
Log.d("TEST", "onActivitySaveInstanceState - ${activity.javaClass.simpleName}")
}
override fun onActivityStopped(activity: Activity) {
Log.d("TEST", "onActivityStopped - ${activity.javaClass.simpleName}")
}
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
Log.d("TEST", "onActivityCreated - ${activity.javaClass.simpleName}")
}
override fun onActivityResumed(activity: Activity) {
Log.d("TEST", "onActivityResumed - ${activity.javaClass.simpleName}")
}
}
}
manifests/AndroidManifest.xml
<application
...
android:name=".AppApplication">
...
</application>
MainActivity
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater).apply {
setContentView(root)
lifecycleOwner = this@MainActivity
}
Toast.makeText(this, AppApplication.message, Toast.LENGTH_SHORT).show()
}
}
참고
반응형
'Development > Android' 카테고리의 다른 글
[Android] Permission (0) | 2021.02.09 |
---|---|
[Android] Notification (0) | 2021.02.09 |
[Android] ViewModel (0) | 2021.02.09 |
[Android] LiveData (0) | 2021.02.09 |
[Android] DataBinding (0) | 2021.02.09 |