Android Tutorial – Start an activity using intent object




We can start an activity with help of startActivity aka Intent. Activity extends with defining method on the context object. Here is a sample code to clearly show how to wakeup or start another activity with intent.

To start an activity, use the method startActivity(intent). This method is defined on theContext object which Activity extends. The following code demonstrates how you can start another activity via an intent.

  • Start the activity connect to the
  • specified class

Sub­activities are started by main activities. Activities with main thread are started by intent and sub­activities are started by parent activities. Main activities are meant and sub­activities are started by meant activities respectively.

Activities which are started by other Android activities are called sub­activities. This wording makes it easier to describe which activity is meant.

In this tutorial we will learn How to Start New Activity On Button Click via Intent

Step 1 – Create new Android project

Step 2 – Add a button to main activity (layout/activity_main.xml) using designer.

 

Step 3 Add the new activity . Right Click Project -> New -> Activity -> Blank Activity

Give the name to your activity  SecondActivity

 

create blank activity
create blank activity

 

Change the background color of SecondActivity if you wish.

 

write the following code

src/main/AndroidManifest.xml

 

layout/activity_main.xml

 

layout/activity_second.xml

 

com/example/programmingknowledge/thefirstapp/MainActivity.java

 

com/example/programmingknowledge/thefirstapp/SecondActivity.java

 

OUTPUT:

layout/activity_main.xml
layout/activity_main.xml
layout/activity_second.xml
layout/activity_second.xml

 

Download Project File

 

 

 

 

 

 

 

 

 

 

 


Partner Sites

VideoToGifs.com

EasyOnlineConverter.com

SqliteTutorials.com