0
0
Android Kotlinmobile~10 mins

Lifecycle awareness in Android Kotlin - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to override the onCreate method in an Android Activity.

Android Kotlin
override fun [1](savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
}
Drag options to blanks, or click blank then click option'
AonCreate
BonStart
ConResume
DonPause
Attempts:
3 left
💡 Hint
Common Mistakes
Using onStart or onResume instead of onCreate.
Forgetting to call super.onCreate.
2fill in blank
medium

Complete the code to log a message when the activity is visible to the user.

Android Kotlin
override fun [1]() {
    super.onStart()
    Log.d("Lifecycle", "Activity is visible")
}
Drag options to blanks, or click blank then click option'
AonStart
BonPause
ConStop
DonDestroy
Attempts:
3 left
💡 Hint
Common Mistakes
Using onResume instead of onStart.
Logging in onCreate instead of onStart.
3fill in blank
hard

Fix the error in the lifecycle method that pauses the activity.

Android Kotlin
override fun [1]() {
    super.onPause()
    // Pause ongoing tasks here
}
Drag options to blanks, or click blank then click option'
AonDestroy
BonStop
ConPause
DonResume
Attempts:
3 left
💡 Hint
Common Mistakes
Using onStop instead of onPause.
Not calling super.onPause.
4fill in blank
hard

Fill both blanks to correctly save and restore instance state in an activity.

Android Kotlin
override fun onSaveInstanceState(outState: Bundle) {
    super.onSaveInstanceState(outState)
    outState.[1]("score", score)
}

override fun onRestoreInstanceState(savedInstanceState: Bundle) {
    super.onRestoreInstanceState(savedInstanceState)
    score = savedInstanceState.[2]("score")
}
Drag options to blanks, or click blank then click option'
AputInt
BgetInt
CputString
DgetString
Attempts:
3 left
💡 Hint
Common Mistakes
Using putString/getString for integer values.
Forgetting to call super methods.
5fill in blank
hard

Fill all three blanks to implement a lifecycle observer that logs when the activity is resumed and paused.

Android Kotlin
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)
    }
}
Drag options to blanks, or click blank then click option'
AonResume
BonPause
CaddObserver
DremoveObserver
Attempts:
3 left
💡 Hint
Common Mistakes
Using removeObserver instead of addObserver.
Overriding wrong lifecycle methods.