Complete the code to override the onCreate method in an Android Activity.
override fun [1](savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}The onCreate method is called when the activity is first created. Overriding it allows you to set up the UI and initialize components.
Complete the code to log a message when the activity is visible to the user.
override fun [1]() { super.onStart() Log.d("Lifecycle", "Activity is visible") }
The onStart method is called when the activity becomes visible to the user.
Fix the error in the lifecycle method that pauses the activity.
override fun [1]() {
super.onPause()
// Pause ongoing tasks here
}The onPause method is called when the activity is partially obscured or about to go into the background. It is the right place to pause tasks.
Fill both blanks to correctly save and restore instance state in an activity.
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.[1]("score", score)
}
override fun onRestoreInstanceState(savedInstanceState: Bundle) {
super.onRestoreInstanceState(savedInstanceState)
score = savedInstanceState.[2]("score")
}Use putInt to save an integer value and getInt to retrieve it from the bundle.
Fill all three blanks to implement a lifecycle observer that logs when the activity is resumed and paused.
class MyObserver : DefaultLifecycleObserver { override fun [1](owner: LifecycleOwner) { Log.d("Observer", "Activity resumed") } override fun [2](owner: LifecycleOwner) { Log.d("Observer", "Activity paused") } fun attach(lifecycle: Lifecycle) { lifecycle.[3](this) } }
The observer overrides onResume and onPause to log messages. It attaches itself to the lifecycle using addObserver.