Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tobiashochguertel/011c15536c08f1efa7f3127e1de045a7 to your computer and use it in GitHub Desktop.
Save tobiashochguertel/011c15536c08f1efa7f3127e1de045a7 to your computer and use it in GitHub Desktop.
Android retrieving a result from an Activity #Android
/* This exercise introduced you to another way of creating user flows using registerFor
ActivityResult. This can be very useful for carrying out a dedicated Task where you need a
result before proceeding with the user’s flow through the app.
*/
private val startForResult = registerForActivityResult(
ActivityResultContracts.StartActivityForResult()
) { activityResult ->
val data = activityResult.data
val backgroundColor = data?.getIntExtra(RAINBOW_COLOR, Color.parseColor(DEFAULT_COLOR)) ?: Color.parseColor(DEFAULT_COLOR)
val colorName = data?.getStringExtra(RAINBOW_COLOR_NAME) ?: ""
val colorMessage = getString(R.string.color_chosen_message, colorName)
val rainbowColor = findViewById<TextView>(R.id.rainbow_color)
rainbowColor.setBackgroundColor(ContextCompat.getColor(this, backgroundColor))
rainbowColor.text = colorMessage
rainbowColor.isVisible = true
}
private fun setRainbowColor(colorName: String, color: Int) {
Intent().let { pickedColorIntent ->
pickedColorIntent.putExtra(RAINBOW_COLOR_NAME, colorName)
pickedColorIntent.putExtra(RAINBOW_COLOR, color)
setResult(
Activity.RESULT_OK,
pickedColorIntent
)
finish()
}
}
// The `setRainbowColor` function creates an intent
// The result is then returned to the calling Activity,
// you call `finish()` so that the calling Activity is displayed.
// The way that you retrieve the rainbow color that the user has chosen is by adding a listener for all the buttons in the layout.

Retrieving a result from an Activity

For some user flows, you will only launch an Activity for the sole purpose of retrieving a result back from it.

This exercise introduced you to another way of creating user flows using registerFor ActivityResult. This can be very useful for carrying out a dedicated Task where you need a result before proceeding with the user’s flow through the app.

  • You’ll be using the registerForActivityResult(ActivityResultContracts.StartActivityForResult()) function to get a result back from the Activity you launch.
// the logic to launch the Activity from the property defined previously
findViewById<Button>(R.id.submit_button).setOnClickListener {
startForResult.launch(
Intent(this, RainbowColorPickerActivity::class.java) // Intent that is launched for its result!
)
}
// This creates an Intent that is launched for its result: `Intent(this, RainbowColor PickerActivity::class.java)`.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_rainbow_color_picker)
val colorPickerClickListener = View.OnClickListener { view ->
when (view.id) {
R.id.red_button -> setRainbowColor(getString(R.string.red), R.color.red)
else -> {
Toast.makeText(this, getString(R.string.unexpected_color), Toast.LENGTH_LONG).show()
}
}
}
// The `colorPickerClickListener` added in the preceding code determines which colors to set for the `setRainbowColor(colorName: String, color: Int)` function by using a `when` statement.
findViewById<View>(R.id.red_button).setOnClickListener(colorPickerClickListener)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment