What is Intent :

An android application can contain zero or more activities. once your application has over one activity, you will got to navigate from one activity to a different activity. In Android, you navigate between activities through what’s called an intent.

The following example It Out shows a way to add another activity to an existing project and so navigate between the 2 activities in android application.

Android Intent Example Steps :

1.  Create new project with name “LinkingActivity”

2.  Open res layout activity_main.xml , add button simple drag drop or  follow this step’s how to add button in Android Button example , after adding new button change its button name  to “Go To Next “.

android intentNote : This example (Project) is developed in Eclipse Version Kepler, tested on Android 4.4.2 (KitKat Version) and SDK build-tools 22.6.3

Now see acitivty_main.xml file code

3.  Now you need second UI for second activity : Right Click on layout folder New ➪ Android XML file (pop up box open see figure)

android intent example

Fill file name “second_activity” and chose Root element as ” LinearLayout” then Click Finish. New UI second_activity.xml created in layout folder.

4.  Now click add text in second_activity.xml  “This is second Activity”

android intent

Code of second_activity.xml

5.  Now Create new class (activity class) : src package name (com.androidinterview.linkingactivity) Right click New   class (pop up open)

android intent example

Fill in name “SecondAcitivity” and click finish, SecondActivity.class created in src – package folder.

6. Now open SecondActivity.class and add following code

7.  After add code in SecondActivity  now add code in MainActivity.class

Main concept of intent and use : intent1 name of intent

8.  Now final step you need to inform application about new activity. So open AndroidMainfest.xml ➪ click “ Application’  Add

android intent example

select “Activity” click ok

android intent example

Click on browse (pop up open )  select “SecondActivity” click  ok   save Project

AndroidMainfest.xml file code :

9.  Done Run your Android Intent Application :
android intent run

Download Linking activity – Android Intent Example Source code :

Leave a Reply