0
0
Android Kotlinmobile~20 mins

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

Choose your learning style9 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.