Android tutorial - Intent android | Intent in android | Android Explicit Intent Example - android app development - android studio - android development tutorial



Intent android

Learn android - android tutorial - Intent android - android examples - android programs

What is Explicit Intent

  • Explicit Intent communicates an activity referring by fully qualified class name. Generally we use Explicit Intent to call an activity because we know the class name of activity which we are calling.
    • They specify the target component by its name. That is, they explicitly specify the target component which needs to respond to it.
    • Android Explicit intent specifies the component to be invoked from activity. In other words, we can call another activity in android by explicit intent.
    • We can also pass the information from one activity to another using explicit intent.
    • android explicit intent sample code
    • Here, we are going to see an example to call one activity from another and vice-versa.

Android calling one activity from another activity example

  • Let's see the simple example of android explicit example that calls one activity from another and vice versa.

activity_main.xml

  • File: activity_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
xmlns:tools="http://schemas.android.com/tools"  
android:layout_width="match_parent"  
android:layout_height="match_parent"  
tools:context=".MainActivity" >  

 <Button  
 android:id="@+id/Button01"  
 android:layout_width="wrap_content"  
 android:layout_height="wrap_content"  
 android:layout_alignParentLeft="true"  
 android:layout_below="@+id/TextView01"  
 android:layout_marginLeft="65dp"  
 android:layout_marginTop="38dp"  
 android:onClick="onClick"  
android:text="Call second activity" />  

 <TextView  
   android:id="@+id/TextView01"  
 android:layout_width="wrap_content"  
   android:layout_height="wrap_content"  
 android:layout_alignLeft="@+id/Button01"  
android:layout_alignParentTop="true"  
 android:layout_marginLeft="18dp"  
android:layout_marginTop="27dp"  
 android:minHeight="60dip"  
 android:text="First Activity"  
android:textSize="20sp" />  
	</RelativeLayout>  
click below button to copy the code from android tutorial team

activitytwo_main.xml

  • File: activitytwo_main.xml
<RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
    xmlns:tools="http://schemas.android.com/tools"  
    android:layout_width="match_parent"  
    android:layout_height="match_parent"  
    tools:context=".MainActivity" >  
  
    <Button  
        android:id="@+id/Button01"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignParentLeft="true"  
        android:layout_below="@+id/TextView01"  
        android:layout_marginLeft="65dp"  
        android:layout_marginTop="38dp"  
        android:onClick="onClick"  
        android:text="Call First activity" />  
  
    <TextView  
        android:id="@+id/TextView01"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:layout_alignLeft="@+id/Button01"  
        android:layout_alignParentTop="true"  
        android:layout_marginLeft="18dp"  
        android:layout_marginTop="27dp"  
        android:minHeight="60dip"  
        android:text="Second Activity"  
        android:textSize="20sp" />  
  
</RelativeLayout>  
click below button to copy the code from android tutorial team

ActivityOne class

  • File: MainActivityOne.java
package com.example.explicitintent2;  
import android.os.Bundle;  
import android.app.Activity;  
import android.content.Intent;  
import android.view.Menu;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.Toast;  
public class ActivityOne extends Activity {  
    /** Called when the activity is first created. */  
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
        Button button1=(Button)findViewById(R.id.Button01);  
          
        button1.setOnClickListener(new OnClickListener(){  
         public void onClick(View view) {  
          Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
          i.putExtra("Value1", "Android By Wikitechy");  
          i.putExtra("Value2", "Simple Tutorial");  
          // Set the request code to any code you like, you can identify the  
          // callback via this code  
          startActivity(i);  
         }  
         });  
      }  
    }   
click below button to copy the code from android tutorial team

ActivityTwo class

  • File: MainActivityTwo.java
package com.example.explicitintent2;  
import android.app.Activity;  
import android.content.Intent;  
import android.os.Bundle;  
import android.view.View;  
import android.view.View.OnClickListener;  
import android.widget.Button;  
import android.widget.EditText;  
import android.widget.TextView;  
import android.widget.Toast;  
public class ActivityTwo extends Activity {  
/** Called when the activity is first created. */  
  @Override  
  public void onCreate(Bundle bundle) {  
    super.onCreate(bundle);  
    TextView tv=new TextView(this);  
    tv.setText("second activity");  
    setContentView(R.layout.activity_two);  
    Bundle extras = getIntent().getExtras();  
    String value1 = extras.getString("Value1");  
    String value2 = extras.getString("Value2");  
    Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+  
         "\n Second Value: "+value2,Toast.LENGTH_LONG).show();  
    Button button1=(Button)findViewById(R.id.Button01);  
    button1.setOnClickListener(new OnClickListener(){  
        public void onClick(View view) {  
            Intent i = new Intent(getApplicationContext(), ActivityOne.class);  
            startActivity(i);  
          }  
    });  
  }  
}   
click below button to copy the code from android tutorial team

Output:

 intent android example


Related Searches to Intent android | Intent in android | Android Explicit Intent Example