0
0
Android Kotlinmobile~20 mins

Activity concept in Android Kotlin - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Activity Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ui_behavior
intermediate
2:00remaining
What happens when an Activity is launched?
Consider an Android app where MainActivity starts SecondActivity. What is the visible result after calling startActivity(Intent(this, SecondActivity::class.java))?
Android Kotlin
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
AThe screen shows SecondActivity UI, and MainActivity is paused in the background.
BThe screen remains on MainActivity, and SecondActivity runs in the background.
CThe app closes and returns to the home screen.
DBoth MainActivity and SecondActivity UI appear side by side.
Attempts:
2 left
💡 Hint
Think about what startActivity does to the current and new activity.
lifecycle
intermediate
1:30remaining
Which lifecycle method is called when an Activity becomes visible?
In Android, when an Activity moves from not visible to visible on screen, which lifecycle method is called?
AonStart()
BonCreate()
ConResume()
DonPause()
Attempts:
2 left
💡 Hint
Visibility means the activity is about to be seen but not yet interactive.
navigation
advanced
1:30remaining
What is the effect of calling finish() inside an Activity?
If you call finish() inside an Activity, what happens next?
Android Kotlin
finish()
AThe current Activity is paused but remains visible.
BThe current Activity is destroyed and removed from the back stack, returning to the previous Activity.
CThe app closes completely.
DNothing happens until the user presses back.
Attempts:
2 left
💡 Hint
Think about what finish() means for the activity lifecycle and navigation stack.
📝 Syntax
advanced
2:00remaining
Identify the correct way to start an Activity with data in Kotlin
Which code snippet correctly starts DetailActivity passing a String extra with key "user" and value "Alice"?
A
val intent = Intent(this, DetailActivity::class.java)
intent.addExtra("user", "Alice")
startActivity(intent)
BstartActivity(Intent(this, DetailActivity::class.java).putExtra(user = "Alice"))
C
val intent = Intent(this, DetailActivity::class.java).apply { putExtra("user", "Alice") }
startActivity(intent)
D
val intent = Intent(this, DetailActivity::class.java)
intent.put("user", "Alice")
startActivity(intent)
Attempts:
2 left
💡 Hint
Check the correct method name and syntax for adding extras to an Intent.
🔧 Debug
expert
3:00remaining
Why does this Activity crash on launch?
Given this Activity code, why does the app crash immediately when launched? class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val button = findViewById
Android Kotlin
class MainActivity : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    val button = findViewById<Button>(R.id.myButton)
    button.setOnClickListener {
      Toast.makeText(this, "Clicked", Toast.LENGTH_SHORT).show()
    }
  }
}
AThe onCreate method is missing a call to super.onStart(), causing lifecycle errors.
BToast.makeText requires applicationContext instead of this as context, causing a crash.
CsetContentView is called after findViewById, so button is null.
DThe layout activity_main.xml does not contain a view with id myButton, so findViewById returns null causing a NullPointerException.
Attempts:
2 left
💡 Hint
Check if the button view exists in the layout before accessing it.