0
0
Android Kotlinmobile~20 mins

Lifecycle awareness in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Lifecycle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
lifecycle
intermediate
2:00remaining
What is the output when the Activity is rotated?
Consider this Android Activity code snippet. What will be printed in the log when the device is rotated (causing configuration change)?
Android Kotlin
class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    Log.d("Lifecycle", "onCreate called")
  }
  override fun onDestroy() {
    super.onDestroy()
    Log.d("Lifecycle", "onDestroy called")
  }
}
AonDestroy called followed by onCreate called
BOnly onCreate called
COnly onDestroy called
DNeither onCreate nor onDestroy called
Attempts:
2 left
💡 Hint
Think about what happens to an Activity during a configuration change like rotation.
ui_behavior
intermediate
2:00remaining
Which lifecycle method is best to start a camera preview?
You want to start the camera preview when the user can see the app screen. Which lifecycle method should you use?
AonStart()
BonCreate()
ConPause()
DonResume()
Attempts:
2 left
💡 Hint
Consider when the app is fully visible and interactive.
🔧 Debug
advanced
2:00remaining
Why does this app crash on rotation?
This Activity crashes when rotated. What is the cause?
Android Kotlin
class MainActivity : AppCompatActivity() {
  private lateinit var textView: TextView
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    textView = findViewById(R.id.textView)
    textView.text = "Hello"
  }
}
AtextView is null because it is not declared
BMissing call to super.onCreate()
CtextView is used before setContentView, so it is not initialized
DsetContentView should be called after onCreate
Attempts:
2 left
💡 Hint
Think about when views are available after setContentView.
navigation
advanced
2:00remaining
What happens to the Activity stack after this navigation?
In an app, MainActivity starts DetailActivity with this code: startActivity(Intent(this, DetailActivity::class.java)) finish() What is the state of the Activity stack after this runs?
AMainActivity is removed, DetailActivity is on top
BBoth MainActivity and DetailActivity are in the stack
COnly MainActivity remains in the stack
DDetailActivity is removed immediately
Attempts:
2 left
💡 Hint
Consider what finish() does after starting a new Activity.
🧠 Conceptual
expert
3:00remaining
What is the correct order of lifecycle callbacks when an Activity goes to background and then returns?
Put these lifecycle methods in the order they are called when the user presses Home (app goes to background) and then returns to the app.
A1,2,4,3,5
B1,2,3,4,5
C2,1,3,4,5
D1,3,2,4,5
Attempts:
2 left
💡 Hint
Remember the lifecycle flow when app goes background and foreground.