Instruction
The Purpose of Passing Data to Other Apps
Consider a real-world example to understand the need to pass data to other apps. Assume you plan to watch a movie in a nearby cinema with your friends, and you book the tickets using an app. When you’ve booked the tickets, you might want to share the booking details such as the film’s name and show time, the cinema’s location, etc., with your friends.
Wouldn’t it be nice if the same app you used to book the tickets let you quickly share those details via your choice of messaging or social media app instead of having to manually type all the details to share them?
Passing data across apps results in better and smoother user experiences. For instance, you might use an app to place a food delivery order, and the app might redirect you to another app to complete the payment for the booking. Or you might use an app to book a dental appointment and then add the appointment details in a calendar or reminder app.
Passing Data Outside Your App
To pass data outside your app, you must use an Intent. You use an Intent in Android to tell the system to perform some action from a component of the same or different app(s); the most common one is to start an Activity. Intents are of various types and can perform different actions based on their action type.
For this lesson, you’ll focus on using an Intent with action type as ACTION_SEND. It can take data as input that must be shared with other apps.
Here’s how you can declare an Intent object in code:
import android.content.Intent
val intent = Intent(Intent.ACTION_SEND)
To pass data to the intent, you must set the type field based on the type of data you’re passing and pass the data using the putExtra(...) function.
Intents deal with a vast number of actions and data types. If you’re interested, you can look further into the official documentation.
For instance, here’s how you can set up the type and data for sharing movie-booking details:
intent.type = "text/plain"
intent.putExtra(Intent.EXTRA_SUBJECT, "Booking Details for Inside Out")
intent.putExtra(Intent.EXTRA_TEXT, "You have booked 2 tickets for Inside Out - 9:30pm at Greenwich Cinema")
You can define other types, too, such as "image/*" or "video/*". Similarly, putExtra(...) supports several other types that correspond to the intent action type, such as EXTRA_PHONE_NUMBER, EXTRA_TIME etc.
To launch an intent, you must use the startActivity() function. Also, to create a chooser so the user can select the app to share the data with, use the Intent.createChooser() function. You must pass the intent and a title for the chooser to that function as follows:
context.startActivity(
Intent.createChooser(
intent,
"Movie booking details"
)
)
Note that the Android system provides the chooser dialog UI.
You can skip the createChooser() method and launch the intent directly:
context.startActivity(intent)
In this case, the Android system will still provide a chooser dialog UI but it’ll lack some of the functionality of the createChooser() dialog such being able to share the data directly to contacts across different apps. Hence, it’s recommended to following createChooser() approach.
Now, it’s time to implement what you’ve learned in an app.