Skip to content
Share
Explore

How to add an in-app “Back to Home” button on the Books screen

Learning outcome: intent-based navigation

📘 LAB: Adding an In-App “Back to Home” Button to the Books Screen

Learning Objective

In this lab, you will add a Back to Home button on the Books screen.
Even though Android devices include a system Back button, a well-designed app often includes its own navigational controls so users always know where they are, and how to get home.
This is a short lab you can complete in 10–15 minutes.

🧭 1. Add a Button to activity_books.xml

Open:
app/src/main/res/layout/activity_books.xml

Add a new Button above the RecyclerView:
<Button
android:id="@+id/btnBackHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back to Home"
android:layout_margin="12dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent" />

If your layout already has constraints for the Add Book button, simply position this one above or beside it as desired.

🧭 2. Wire the Button in BookListActivity.kt

Open:
app/src/main/java/.../BookListActivity.kt

Inside onCreate, add:
findViewById<Button>(R.id.btnBackHome).setOnClickListener {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish() // Optional: Prevents stacking multiple Activities on the back stack
}

This gives your Books screen an explicit navigation path back to Home.
Why finish()? It closes the current screen so pressing system Back does NOT bring you back to the list again. This keeps navigation clean.

🧭 3. Test the Navigation

Run the app.
From the main screen, open Books.
Tap Back to Home.
You should return cleanly to the Main screen.
Pressing the device/system Back button from Home should not take you back into Books — correct behavior.

🧠 Conceptual Debrief

The system Back button navigates backward through the Activity stack.
An in-app Back button provides intentional navigation where you (the app designer) control the flow.
You can duplicate this pattern for:
Customers
Orders
Edit/Add screens
Once you’ve done one, you can copy/paste the pattern.

🏁 End State

By completing this lab, you can now:
✔ Add an in-app navigation control ✔ Wire the button to start a new Activity ✔ Manage the back stack with finish() ✔ Improve the app’s UX consistency

Want to print your doc?
This is not the way.
Try clicking the ··· in the right corner or using a keyboard shortcut (
CtrlP
) instead.