Challenge - 5 Problems
Lifecycle Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ lifecycle
intermediate2: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") } }
Attempts:
2 left
💡 Hint
Think about what happens to an Activity during a configuration change like rotation.
✗ Incorrect
When the device rotates, the current Activity is destroyed and recreated. So onDestroy is called first, then onCreate is called again.
❓ ui_behavior
intermediate2: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?
Attempts:
2 left
💡 Hint
Consider when the app is fully visible and interactive.
✗ Incorrect
onResume() is called when the Activity is in the foreground and ready for user interaction, so it's the best place to start camera preview.
🔧 Debug
advanced2: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" } }
Attempts:
2 left
💡 Hint
Think about when views are available after setContentView.
✗ Incorrect
textView is accessed before setContentView, so it is not yet linked to the layout and causes a crash.
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?
Attempts:
2 left
💡 Hint
Consider what finish() does after starting a new Activity.
✗ Incorrect
finish() removes MainActivity from the stack, so only DetailActivity remains on top.
🧠 Conceptual
expert3: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.
Attempts:
2 left
💡 Hint
Remember the lifecycle flow when app goes background and foreground.
✗ Incorrect
When going to background: onPause then onStop. When returning: onRestart, onStart, onResume.