Start New Activity Example

Posted By on March 23, 2016


Download PDF
Execution Control Flow of an Android Application
Passing data between activities in Android with Example

Here we will learn about Android Start New Activity Example.  This is the simplest way to start a new activity in android. First we will create  a new Android Activity and then start a new android activity from another Activity. Also we can start a new android activity from any other components (like from Android Services, Button click etc). To make the example very clear, We will create a new activity and start the activity from another activity’s button click event. We can also find the complete code description here.

Android Start New Activity Example Screen Shots

android-start-new-activity  android-new-activity

Android Code Snippet to Start a new Activity

That’s it. In the above code snippet Android Activity Manager will start your requested Activity. If your Activity code if ready, then you can put the above code snippet in your code from where you want to start a Activity. Please make sure to replace the Activity name (TestActivity) with your Activity name from the above code snippet. Also make sure to add your Activity’s name in your app’s Manifest file. If you are still facing any issue then don’t worry below you can download complete code with description.

Android Start New Activity Example Details

In the Application we will create 2 Android Activities. One will have a simple button from where we will start the new Activity. For an Android Activity, we need two parts (One is the xml layout file and other is the Activity code logic in the java file). So let’s start.

1. Layout xml File (activity_main.xml) for MainActivity

This xml layout file will be used for showing GUI for our MainActivity, So we have added below GUI options in this xml layout file.

1. One a Android TextView (for showing simple information). The text view is optional.

2. One Android Button (we will start the new activity from this button click option)

2. Layout xml File (activity_test.xml) for New Activity

This simple xml layout file will be used for showing GUI for our new Activity, which will be start from the main Activity. Here we have added one simple TextView to show some information on the new Activity.

 3. MainActivity.Java File

This activity is the main activity from where we will start the new activity.

 4. TestActivity.Java

This simple java file contains the code for your new Activity, which we are going to launch from the MainActivity.

 5. Manifest File

We need to add each android Activity name in this file, which we are using in our application. So Below is the Manifest file in which we have added (both MainActivity and TestActivity ) names.

Note: If you will forget to add your Activity name in the Manifest file, then Android will not found it in the package and will show Force Close Error, when you will run the application.

Execution Control Flow of an Android Application
Passing data between activities in Android with Example

Download PDF

Posted by Akash Kurup

Founder and C.E.O, World4Engineers Educationist and Entrepreneur by passion. Orator and blogger by hobby

Website: http://world4engineers.com