Explicit Intent

Now, lets see how the components in android communicate.

Communication between the three core components in android-activity, service and broadcast receiver is made possible using intents. Intents can be said as messages passed between these components. An Intent object is a bundle of information containing information such as the action to be performed, data on which the action is to be performed etc.

Intents are of two types: Explicit intents and Implicit intents

Here , in this post, I’m discussing about Explicit Intent.

Explicit intents: They specify the target component by its name.That is, they explicitly specify the target component which needs to respond to it.

Here is how it is done:

Intent intent=new Intent(source.this,destination.class);

Or

Intent intent=new Intent();
Intent.setClass(source.this,destination.class);

Lets see what an explicit intent is and how it works through a simple example.

Code for the Source activity :

public class Source extends Activity {
/** Called when the activity is first created. */
Button dest;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dest=(Button)findViewById(R.id.click);
dest.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
 Intent intent=new Intent(Source.this,Destination.class);
                        startActivity(intent);
}
});
}
}

The XML code for main.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click to go to destination"
android:textSize="20dip" />
</LinearLayout>

This is how the UI looks:

Code for the Destination activity:

public class Destination extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.maindest);
}
}

XML code for maindest.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"
android:background="#FFF">
<TextView
android:id="@+id/textView1"
android:layout_width="278dp"
android:layout_height="wrap_content"
android:text="Welcome to Destination"
android:textSize="25dip"
android:textColor="#0000ff" />
</LinearLayout>

The Output When the Button on the Source UI is clicked :

Hope this helped you in understanding an Intent and how it works.

Advertisements

, , , , , , ,

  1. #1 by Ida on October 7, 2012 - 9:32 pm

    Excellent tutorial. If there is one thing that is difficult to find good information on as a beginner, it’s how buttons work and how to make the click of a button lead to a new page. Thank you!

  2. #2 by meenu on October 19, 2012 - 2:23 pm

    good…

  3. #3 by meenu on October 19, 2012 - 8:21 pm

    one more thing,
    even though this is just an example of intent, you should have used finish method( i am not sure,as i am a beginner).Though app worked properly i faced problems in closing the destination activity.So it would have been complete if you added methods to close the activities as well…

  4. #4 by Sanketh Salian on December 24, 2013 - 11:22 pm

    wht do i do if i need mutiple button on the same page which leads to different page

  5. #5 by Priyanka on February 22, 2015 - 4:41 pm

    Thank you so much..it heped me…

  1. How to: What is Intent in Android? | SevenNet
  2. Answer: What is Intent in Android? #answer #it #programming | IT Info
  3. What is Intent in Android? - DexPage

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: