Bird
Raised Fist0
Android Kotlinmobile~20 mins

Passing arguments in Android Kotlin - Mini App: Build & Ship

Choose your learning style10 modes available
Build: Greeting Screen
This screen shows a greeting message using a name passed as an argument from another screen.
Target UI
-----------------------
| Greeting Screen      |
|---------------------|
| Hello, [Name]!       |
|                     |
| [Go to Greeting]    |
-----------------------
Create two screens: MainScreen and GreetingScreen.
MainScreen has a button labeled 'Go to Greeting'.
When the button is tapped, navigate to GreetingScreen passing a name string argument.
GreetingScreen displays 'Hello, [Name]!' using the passed argument.
Use Android Jetpack Navigation component or Intent extras to pass the argument.
Starter Code
Android Kotlin
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView

class MainScreen : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val button = findViewById<Button>(R.id.goGreetingButton)
    // TODO: Set button click listener to navigate to GreetingScreen with argument
  }
}

class GreetingScreen : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_greeting)

    val greetingText = findViewById<TextView>(R.id.greetingTextView)
    // TODO: Get the name argument and display greeting
  }
}
Task 1
Task 2
Solution
Android Kotlin
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import android.widget.Button
import android.widget.TextView

class MainScreen : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val button = findViewById<Button>(R.id.goGreetingButton)
    button.setOnClickListener {
      val intent = Intent(this, GreetingScreen::class.java)
      intent.putExtra("name", "Alice")
      startActivity(intent)
    }
  }
}

class GreetingScreen : AppCompatActivity() {
  override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_greeting)

    val greetingText = findViewById<TextView>(R.id.greetingTextView)
    val name = intent.getStringExtra("name") ?: "Guest"
    greetingText.text = "Hello, $name!"
  }
}

We created two screens: MainScreen and GreetingScreen. In MainScreen, we set a click listener on the button. When clicked, it creates an Intent to start GreetingScreen and passes a string extra with key "name" and value "Alice".

In GreetingScreen, we retrieve the passed name from the intent extras. If no name is passed, we use "Guest" as default. Then we update the TextView to show a greeting message with the name.

This shows how to pass simple data between screens in Android using Intent extras.

Final Result
Completed Screen
-----------------------
| Greeting Screen      |
|---------------------|
| Hello, Alice!        |
|                     |
| [Back]              |
-----------------------
User taps 'Go to Greeting' button on MainScreen.
App navigates to GreetingScreen showing 'Hello, Alice!'.
User can press back to return to MainScreen.
Stretch Goal
Modify the app to pass the name argument using Android Jetpack Navigation component's Safe Args.
💡 Hint
Use navigation graph XML to define argument and use generated directions class to pass the argument safely.

Practice

(1/5)
1. What is the primary purpose of passing arguments between activities in Android Kotlin?
easy
A. To close the current activity
B. To change the app theme
C. To share data between different screens
D. To update the app version

Solution

  1. Step 1: Understand activity communication

    Activities often need to share information like user input or selections.
  2. Step 2: Purpose of passing arguments

    Passing arguments allows one screen to send data to another screen.
  3. Final Answer:

    To share data between different screens -> Option C
  4. Quick Check:

    Passing arguments = sharing data [OK]
Hint: Passing arguments means sending data between screens [OK]
Common Mistakes:
  • Thinking it changes app theme
  • Confusing with app version updates
  • Assuming it closes activities
2. Which Kotlin code correctly passes a String argument "username" with value "Alice" to another activity using Intent?
easy
A. val intent = Intent(this, NextActivity::class.java) intent.putExtra("username", "Alice") startActivity(intent)
B. val intent = Intent(this, NextActivity::class.java) intent.putString("username", "Alice") startActivity(intent)
C. val intent = Intent(this, NextActivity::class.java) intent.addExtra("username", "Alice") startActivity(intent)
D. val intent = Intent(this, NextActivity::class.java) intent.setExtra("username", "Alice") startActivity(intent)

Solution

  1. Step 1: Identify correct Intent method

    The method to add data is putExtra(key, value).
  2. Step 2: Check method names in options

    Only val intent = Intent(this, NextActivity::class.java) intent.putExtra("username", "Alice") startActivity(intent) uses putExtra correctly; others use invalid method names.
  3. Final Answer:

    val intent = Intent(this, NextActivity::class.java)\nintent.putExtra("username", "Alice")\nstartActivity(intent) -> Option A
  4. Quick Check:

    Use putExtra() to pass data [OK]
Hint: Use putExtra() to add data to Intent [OK]
Common Mistakes:
  • Using non-existent methods like putString or addExtra
  • Forgetting to call startActivity()
  • Passing wrong argument types
3. What will be the output of the following code in the receiving activity?
val username = intent.getStringExtra("username")
textView.text = username ?: "Guest"
If the sending activity did NOT pass any "username" extra?
medium
A. The app will crash with NullPointerException
B. The TextView will show "null"
C. The TextView will be empty
D. The TextView will show "Guest"

Solution

  1. Step 1: Understand getStringExtra behavior

    If no extra is passed, getStringExtra returns null.
  2. Step 2: Analyze Elvis operator usage

    The expression username ?: "Guest" means if username is null, use "Guest".
  3. Final Answer:

    The TextView will show "Guest" -> Option D
  4. Quick Check:

    Null fallback with ?: operator = "Guest" [OK]
Hint: ?: operator provides default if null [OK]
Common Mistakes:
  • Expecting null string to display
  • Assuming app crashes on null
  • Forgetting ?: fallback
4. Identify the error in this code snippet that tries to retrieve an Int extra named "age":
val age = intent.getIntExtra("age")
textView.text = age.toString()
medium
A. getIntExtra requires a default value as second argument
B. getIntExtra should be getIntExtras
C. age.toString() is invalid for Int
D. Intent cannot carry Int values

Solution

  1. Step 1: Check getIntExtra method signature

    getIntExtra requires two arguments: key and default value.
  2. Step 2: Identify missing argument

    The code misses the default value argument, causing a compile error.
  3. Final Answer:

    getIntExtra requires a default value as second argument -> Option A
  4. Quick Check:

    getIntExtra(key, default) needs default [OK]
Hint: Always provide default value for getIntExtra [OK]
Common Mistakes:
  • Omitting default value argument
  • Using wrong method name getIntExtras
  • Thinking Int can't be passed
5. You want to pass a list of Strings named "items" from one activity to another. Which is the correct way to pass and retrieve this list using Intent?
hard
A. Use intent.putExtra("items", items) and intent.getStringExtra("items")
B. Use intent.putStringArrayListExtra("items", items) and intent.getStringArrayListExtra("items")
C. Use intent.putExtra("items", items.toString()) and intent.getStringExtra("items")
D. Use intent.putExtra("items", items) and intent.getSerializableExtra("items")

Solution

  1. Step 1: Passing a list of Strings

    For ArrayList<String>, use putStringArrayListExtra to pass the list.
  2. Step 2: Retrieving the list

    Use getStringArrayListExtra to get the list back safely.
  3. Final Answer:

    Use intent.putStringArrayListExtra("items", items) and intent.getStringArrayListExtra("items") -> Option B
  4. Quick Check:

    Use put/getStringArrayListExtra for String lists [OK]
Hint: Use put/getStringArrayListExtra for String lists [OK]
Common Mistakes:
  • Passing list as plain putExtra
  • Converting list to string incorrectly
  • Using getSerializableExtra without casting
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard