Working with Intents: Navigation and Data Sharing in Android
![]() |
Photo by Susan Q Yin on Unsplash |
What Are Intents?
An Intent is a messaging object that allows communication between Android components such as Activities, Services, and Broadcast Receivers.
Key Purposes of Intents in Android:
- Launching Activities: Intents are primarily used to start new activities or navigate between screens in an app.
- Starting Services: Intents can be used to start or bind to services, allowing background tasks such as data fetching or computation to occur.
- Broadcasting Messages: Intents can be sent as broadcast messages, which can be received by other apps or system components, like notifications or system events.
- Inter-App Communication: Through implicit intents, apps can request other apps to handle tasks such as opening a URL or sending a message.
There are two main types of Intents:
- Explicit Intents: Directly specify the target component.
- Implicit Intents: Specify an action to be performed without identifying the target component explicitly.
Using Explicit Intents
Explicit Intents specify the exact component (Activity, Service, or BroadcastReceiver) that should handle the intent. You specify the class name of the target component explicitly.
Use Cases:
- Navigating between activities (e.g., opening a new screen from a button click).
- Starting a service or sending data to a service.
- Sending data to a broadcast receiver.
Example: Navigating Between Two Activities
-
Create Two Activities:
-
MainActivity.kt
-
SecondActivity.kt
-
-
Design the Layouts:
- activity_main.xml
<Button android:id="@+id/navigateButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Go to Second Activity" />
- activity_second.xml
<TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to the Second Activity!" />
-
Add Navigation Logic in
MainActivity.kt
:
val navigateButton: Button = findViewById(R.id.navigateButton) navigateButton.setOnClickListener { val intent = Intent(this, SecondActivity::class.java) startActivity(intent) }
With this setup, clicking the button navigates to the second Activity.
Exercise:
-
Add a button in
SecondActivity
to navigate back toMainActivity
. -
Pass a simple message from
MainActivity
toSecondActivity
and display it in aTextView
.
Using Implicit Intents
Implicit Intents do not specify a specific component. Instead, they declare a general action to be performed. The Android system then determines the best component to handle the intent based on the provided action, data, and categories.
Use Cases:
- Opening a web page using the browser.
- Sending an email.
- Viewing or editing a contact.
- Sharing data between apps.
Example: Opening a Webpage
val openWebButton: Button = findViewById(R.id.openWebButton) openWebButton.setOnClickListener { val webpage: Uri = Uri.parse("https://www.example.com") val intent = Intent(Intent.ACTION_VIEW, webpage) startActivity(intent) }
Here, Intent.ACTION_VIEW
signals that you want to view the
webpage specified by the Uri
.
Example: Sharing Text
val shareButton: Button = findViewById(R.id.shareButton) shareButton.setOnClickListener { val intent = Intent(Intent.ACTION_SEND) intent.type = "text/plain" intent.putExtra(Intent.EXTRA_TEXT, "Check out this cool app!") startActivity(Intent.createChooser(intent, "Share via")) }
Exercise:
-
Create a button to send an email using
Intent.ACTION_SENDTO
. - Use an implicit Intent to open the dialer with a predefined phone number.
Passing Data Between Activities
Intents can also carry data using key-value pairs known as "extras."
Example: Sending Data to Another Activity
-
Send Data from
MainActivity.kt
:val intent = Intent(this, SecondActivity::class.java) intent.putExtra("USER_NAME", "John Doe") startActivity(intent)
-
Receive Data in
SecondActivity.kt
:val userName = intent.getStringExtra("USER_NAME") val textView: TextView = findViewById(R.id.textView) textView.text = "Welcome, $userName!
Exercise:
- Pass multiple pieces of data (e.g., username and age) and display them in the second Activity.
Real-World Use Case: Login and Profile Navigation
Let’s create a simple flow where a user logs in and navigates to their profile screen.
Step 1: Create Login and Profile Activities
-
LoginActivity: Includes
EditText
fields for username and password, and a login button. - ProfileActivity: Displays a welcome message with the username.
Step 2: Design Layouts
-
activity_login.xml:
<EditText android:id="@+id/usernameInput" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Enter Username" /> <Button android:id="@+id/loginButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login" />
-
activity_profile.xml:
<TextView android:id="@+id/welcomeText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" />
Step 3: Implement Logic
-
LoginActivity.kt:
val loginButton: Button = findViewById(R.id.loginButton) loginButton.setOnClickListener { val username = findViewById<EditText>(R.id.usernameInput).text.toString() val intent = Intent(this, ProfileActivity::class.java) intent.putExtra("USERNAME", username) startActivity(intent) }
-
ProfileActivity.kt:
val username = intent.getStringExtra("USERNAME") val welcomeText: TextView = findViewById(R.id.welcomeText) welcomeText.text = "Welcome, $username!"
Summary
In this step, you learned:
- Explicit Intents: Navigate between specific components in your app.
- Implicit Intents: Perform actions like opening a webpage or sharing data.
- Data Sharing: Pass information between Activities using Intents.
What’s Next?
In the next step, we’ll dive into Fragments — a powerful way to create dynamic and reusable UIs in your Android apps. Stay tuned and don’t forget to subscribe for updates!
- Step 1: Master the Basics of Kotlin Programming for Android Development
- Step 2: Setting Up Your Development Environment
- Step 3: Understanding Android Basics — Activities, Layouts, and Views
- Step 4: Working with Intents in Android
- Next -> Step 5: Fragments — Dynamic and Modular UI Design
- Step 6: RecyclerView and Adapters — Displaying Lists of Data
- Step 7: ViewModel and LiveData — Building Reactive Apps
- Step 8: Room Database — Local Data Storage
- Step 9: Networking with Retrofit — Fetching and Sending Data
- Step 10: Material Design — Enhancing User Experience
- Step 11: Firebase Integration — Real-time Data and Authentication
- Step 12: Testing and Debugging — Ensuring App Stability
- Step 13: Publishing Your App — Taking It Live.