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.
- package com.example.administrator.intentexampleapp;
- import android.support.v7.app.ActionBarActivity;
- import android.os.Bundle;
- import android.view.Menu;
- import android.view.MenuItem;
- public class MainActivity extends ActionBarActivity
- {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
-
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
-
-
-
- int id = item.getItemId();
-
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
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.
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.administrator.intentexampleapp" >
-
- <application
- android:allowBackup="true"
- android:icon="@mipmap/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name=".MainActivity"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
-
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity
- android:name=".MySecondActivity"
- android:label="@string/title_activity_my_second" >
- </activity>
- </application>
-
- </manifest>
One more statement is added in this file.
- <Intent-filter>
- <action android:name="android.intent.action.MySecondActivity" />
- <category android:name="android.intent.category.DEFAULT" />
- </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.
- <resources>
- <string name="app_name">IntentExampleApp</string>
-
- <string name="hello_world">Hello world!</string>
- <string name="Second_Activity">Click to show Second Activity</string>
- <string name="MySecond_Activity">This is my Second Activity</string>
- <string name="action_settings">Settings</string>
- <string name="title_activity_my_second">MySecondActivity</string>
- </resources>
In activity_main.xml file, add the method name showSecondActivity for onClick event and implement this method in MainActivity.java class file.
- public void showSecondActivity(View v)
- {
- Intent intent= new Intent(this,MySecondActivity.class);
- startActivity(intent);
- }
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.