Intent Class | Android
INTENT CLASS
Hello all,
Today we are going to focus on the intent class in android.
The main purpose of an Intent is to call other activities. i.e., when one activity wants to call another activity Intent class comes into play.
Intents allow the activities to request function from other Android activities.
It allows activities of the same application get interacted with the both the same application or even other applications
Intent class is a data structure generally used for two main purposes
- Calling another activity to perform a particular function.
- Event notifications
Though for a beginner understanding the fields in an intent is a bit difficult, just get to know what the fields are and understand their importance
INTENT FIELDS
Name | Description |
Action | This field is used to represent the operation that is to be perform |
Data | Data is associated with Intent ;formatted as URI(Uniform Resource Identifier) |
Category | Additional information about the component that can handle the intent |
Type | Specifies the mime type of the intent’s data |
Component | This field is to identify the target activity |
Extras | This field stores additional information associated with the intent. |
Flags | Specify the information about how the intent should be handled |
How to Start Activities using Intents
One can start an activity using the following methods
- StartActivity(Intent intent,…)
- startActivityForResult(Intent intent, …)
Android uses two ways to find out the target activity when either of the two methods mentioned previously is used
- EXPLICITLY –(To be called)Target activity is explicitly mentioned in the intent’s component
- IMPLICITLY – (If the activity is not mentioned explicitly) Android determines based on the properties of the activities intent that was used for.
Today lets just focus on Explicit calling
Example of an Application that explicitly starts another activity
Consider two activities,
- MainActivity – This is the first activity called that has a button that directs to HelloAndroidActivity
- HelloAndroidActivity– This is the target activity which simply displays a text
MainActivity is the first activity which is started. It is a welcome screen has a simple “Welcome” text and a button, which on clicked directs to HelloAndroidActivity activity
And yes, this is done by using Intent in the first Activity, MainActivity. Intent calls the second activity using the following syntax :
STEPS
- Create a new Android Application Project with a Blank Activity named as MainActivity and layout file named as activity_main.xml.
- Please find the following java files and respective xml layout files
MainActivity.java : ( src/com/MainActivity.java)
package com.example.androidexample; import android.app.Activity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity { Button button1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button1=(Button)findViewById(R.id.button1); button1.setOnClickListener(new OnClickListener(){ @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent=new Intent(MainActivity.this,HelloAndroidActivity.class); startActivity(intent); } }); } }
- xml(res/layout/activity_main.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome to First page" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:text="Go to Next Page" /> </LinearLayout>
- HelloAndroidActivity.java(src/com/HelloAndroidActivity.java)
package com.example.androidexample; import android.app.Activity; import android.os.Bundle; public class HelloAndroidActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.second_activity); } }
- second_activity.xml (res/layout/second_activity.xml)
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello Android!!!" /> </LinearLayout>
Last but not the least make sure your AndroidManifest.xml has declared both the activities
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.androidexample" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="21" /> <application android:allowBackup="true" android:icon="@drawable/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=".HelloAndroidActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.NORMAL" /> </intent-filter> </activity> </application> </manifest>
Thus we have learned some basics on Intent class and how to explicitly call activities using Intent class.
Will keep posting more. Please feed in your suggestions as a comment.
Related Posts
Latest posts by abirami vijayakumar (see all)
- Views & View Events | Android - March 26, 2015
- Intent Class | Android - January 2, 2015
- Android Activity Lifecycle Methods - December 19, 2014