Intent In Android

Overview

We have seen in the previous articles about creating an environment for the development of Android Applications, and activity life cycles in Android Applications, etc. Kindly refer to the articles, given below:

Introduction

In this article, we will learn about Intent. An Android Application may have zero or more activities. When your application has more than one activity, you often need to navigate from the one activity to another activity. This is Intent, through which you navigate between the activities.

Coding

For this, create a new project IntentExampleApp. By default, MainActivity.java class is added in the project.

  

  1. package com.example.administrator.intentexampleapp;  
  2. import android.support.v7.app.ActionBarActivity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.MenuItem;  
  6. public class MainActivity extends ActionBarActivity  
  7. {  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.     }  
  13.     @Override  
  14.     public boolean onCreateOptionsMenu(Menu menu) {  
  15.         // Inflate the menu; this adds items to the action bar if it is present.  
  16.         getMenuInflater().inflate(R.menu.menu_main, menu);  
  17.         return true;  
  18.     }  
  19.     @Override  
  20.     public boolean onOptionsItemSelected(MenuItem item) {  
  21.         // Handle action bar item clicks here. The action bar will  
  22.         // automatically handle clicks on the Home/Up button, so long  
  23.         // as you specify a parent activity in AndroidManifest.xml.  
  24.         int id = item.getItemId();  
  25.         //noinspection SimplifiableIfStatement  
  26.         if (id == R.id.action_settings) {  
  27.             return true;  
  28.         }  
  29.         return super.onOptionsItemSelected(item);  
  30.     }  
  31. }  

Add the second activity called MySecondActivity by right clicking on Java->New->Activity->Blank Activity.

  
 
 

As soon as a new activity is added in the project, the reference of the activity is also added in the AndroidManifest.xml file.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2.  <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.      package="com.example.administrator.intentexampleapp" >  
  4.    
  5.      <application  
  6.          android:allowBackup="true"  
  7.          android:icon="@mipmap/ic_launcher"  
  8.          android:label="@string/app_name"  
  9.          android:theme="@style/AppTheme" >  
  10.          <activity  
  11.              android:name=".MainActivity"  
  12.              android:label="@string/app_name" >  
  13.              <intent-filter>  
  14.                  <action android:name="android.intent.action.MAIN" />  
  15.    
  16.                  <category android:name="android.intent.category.LAUNCHER" />  
  17.              </intent-filter>  
  18.          </activity>  
  19.          <activity  
  20.              android:name=".MySecondActivity"  
  21.              android:label="@string/title_activity_my_second" >  
  22.          </activity>  
  23.      </application>  
  24.    
  25.  </manifest>  

One more statement is added in this file.

  1. <Intent-filter>  
  2.      <action android:name="android.intent.action.MySecondActivity" />  
  3.      <category android:name="android.intent.category.DEFAULT" />  
  4.  </Intent-filter>  
This is used to invoke the second activity, using the name MySecondActivity. In the string.xml file under layout folder, add one string variable MySecond_Activity to be displayed as a text in the second activity.
  1. <resources>  
  2.      <string name="app_name">IntentExampleApp</string>  
  3.    
  4.      <string name="hello_world">Hello world!</string>  
  5.      <string name="Second_Activity">Click to show Second Activity</string>  
  6.      <string name="MySecond_Activity">This is my Second Activity</string>  
  7.      <string name="action_settings">Settings</string>  
  8.      <string name="title_activity_my_second">MySecondActivity</string>  
  9.  </resources>  
In activity_main.xml file, add the method name showSecondActivity for onClick event and implement this method in MainActivity.java class file.
  1. public void showSecondActivity(View v)  
  2.  {  
  3.       Intent intent= new Intent(this,MySecondActivity.class);  
  4.       startActivity(intent);  
  5.  }  
Run the application by clicking Shift + F10.

 

When you click the button, the second activity opens.



Explanation

When the user clicks the button, the method showSecondActivity is called. In this method, an object of Intent is created and this is a type of an Explicit Intent. This Intent provides the external class to be invoked.The startActivity() method invokes another activity only. It does not return a result to the current activity.

Conclusion

In this article, we learned about Intent, which is of Explicit type. In the next article, I will explain about returning the results and passing the data from Intent object.

Up Next
    Ebook Download
    View all
    Learn
    View all