How to start new activity on button click
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
In Android app development, activities play a crucial role in managing the user interface. An activity represents a single screen with a user interface—akin to a window or page in a desktop application. The most common interaction that requires creating or transitioning to a new activity is through a button click. This article explores how to start a new activity upon a button click using technical explanations and examples.
Understanding Activities and Intents
Activities in Android are essential building blocks of an Android app. Each activity usually interacts with the user and performs a specific task. To move from one activity to another, developers use Intents. An Intent is an abstract description of an operation to be performed, such as starting or switching to a different activity.
Intents in Android
An Intent is generally used to start activities and can be of two types:
- Explicit Intents: Directly specifies the activity you want to start.
- Implicit Intents: Describes an action to be performed, and the Android system determines the app component to fulfill that intent.
Steps to Start a New Activity on Button Click
Here’s a step-by-step guide to set up a transition to a new activity when a button is clicked.
Step 1: Set Up Your Activities
- Create your Main Activity: This will be the launch activity with a user interface that includes a button. For instance,
MainActivity. - Create the Second Activity:
- Within your Android Studio project, create a new activity, say
SecondActivity. - Define the UI layout for
SecondActivityin a new XML file (e.g.,activity_second.xml).
Step 2: Update Layout XML for Main Activity
You need to have a button in your activity_main.xml layout file:
Step 3: Define Intent in Main Activity
In MainAcitvity.java, create an Intent and start SecondActivity on the button click:
Step 4: Declare New Activity in AndroidManifest.xml
Every activity in an Android app must be declared in the AndroidManifest.xml file:
Additional Considerations
Passing Data Between Activities
If you need to pass data from MainActivity to SecondActivity, you can use Intent extras:
Finish Activity
Optionally, when you start a new activity, you might want to finish the current one:
This line of code, finish(), will close the current activity and remove it from the activity stack.
Summary Table
The table below summarizes the key points of starting a new activity via button click:
| Step | Description | Key Actions |
| 1 | Create Activities | Create Main and Second Activities. Define UI layout for both. |
| 2 | Update Main Layout | Add a Button element in activity_main.xml. |
| 3 | Configure Intent | Set up an Intent to start the new activity. |
| 4 | Manifest Declaration | Declare all activities in AndroidManifest.xml. |
| Additional | Pass Data Finish Current | Use Intent extras to pass data.
Use finish() if necessary. |
Understanding how to navigate between activities is a foundational skill in Android development. Practicing these steps will ensure smooth transitions within your application and enhance the user experience.
Related reading
- How to start spring-boot app without depending on Database?
- How to start Spring Boot app in Spock Integration Test
- How to start up spring-boot application via command line?
- How to start up spring-boot application via command line?
- How to stop EditText from gaining focus when an activity starts in Android?
- How to stop unwanted UIButton animation on title change?
- How to stop a thread created by implementing runnable interface?
- How to store printStackTrace into a string

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.