Appendix A. Leftovers: The Top Ten Things (we didn’t cover)
Even after all that, there’s still a little more.
There are just a few more things we think you need to know. We wouldn’t feel right about ignoring them, and we really wanted to give you a book you’d be able to lift without extensive training at the local gym. Before you put down the book, read through these tidbits.
1. Sharing data with other apps
If you want to share simple data with another app, you can do so with an Intent. You can think of an Intent as an “intent to do something.” It’s a type of message that lets you send data to another object—such as an activity—at runtime.
Sharing data with Android’s intent resolver
If you want to pass text to another activity, you can do so using code like this:
val sendIntent: Intent = Intent().apply {
action = Intent.ACTION_SEND
putExtra(Intent.EXTRA_TEXT, "This is some text.")
type = "text/plain"
}
startActivity(sendIntent)
The code first creates an Intent named sendIntent. It uses the type property to specify the type of data that’s being sent (in this case plain text), and uses putExtra() to attach the data (here, it’s some text).
The action property tells Android which types of activity can receive the intent. Here, it’s set using:
action = Intent.ACTION_SEND
which ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access