How to start an activity from another activity in Android

0
1077

In our blog today you will learn to create a new activity from another activity. We have the main activity called as “StartNewActivityActivity.java”. It will call two activities namely “ChildActivity.java” and “second_child.java”. ChildActivity is a simple activity that just displays a message. “second_child” demonstrates the method of passing values to activities by calling another activity “SecondChildResultActivity.java”. The result from “second_child.java” is passed onto SecondChildResultActivity.java. Let us start writing the code

Step 1

Create an Android Project and name it “StartNewActivity”. Select the Android SDK version of your choice. Once the project gets created open the “StartNewActivityActivity.java” and write the code below

Update the main.xml files as follows

Step 2

Create “ChildActivity.java” under src folder and add the following

Step 3

Update the child_Layout.xml with the following

Step 4

Create and update the second_child.java and second_child.xml

Step 5

Create and update the SecondChildResultActivity.java and second_child_result.xml

Step 6

Make sure your Android manifest file looks like the following

You are now ready to compile and execute the code. The output will look like the following

Output

main_screen

Child_image

Child2_image

child3_image