Appendix A. Leftovers: The Top Ten Things (we didn’t cover)

image

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).

image

The action property tells Android which types of activity can receive the intent. Here, it’s set using:

action ...

Get Head First Android Development, 3rd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.