AdSense

AdSense3

Tuesday, 11 August 2015

Android Explicit Intent Example

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.
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
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/Button01"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_below="@+id/TextView01"  
  13.         android:layout_marginLeft="65dp"  
  14.         android:layout_marginTop="38dp"  
  15.         android:onClick="onClick"  
  16.         android:text="Call second activity" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/TextView01"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignLeft="@+id/Button01"  
  23.         android:layout_alignParentTop="true"  
  24.         android:layout_marginLeft="18dp"  
  25.         android:layout_marginTop="27dp"  
  26.         android:minHeight="60dip"  
  27.         android:text="First Activity"  
  28.         android:textSize="20sp" />  
  29.   
  30. </RelativeLayout>  

activitytwo_main.xml

File: activitytwo_main.xml
  1. <RelativeLayout xmlns:androclass="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/Button01"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:layout_alignParentLeft="true"  
  12.         android:layout_below="@+id/TextView01"  
  13.         android:layout_marginLeft="65dp"  
  14.         android:layout_marginTop="38dp"  
  15.         android:onClick="onClick"  
  16.         android:text="Call First activity" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/TextView01"  
  20.         android:layout_width="wrap_content"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_alignLeft="@+id/Button01"  
  23.         android:layout_alignParentTop="true"  
  24.         android:layout_marginLeft="18dp"  
  25.         android:layout_marginTop="27dp"  
  26.         android:minHeight="60dip"  
  27.         android:text="Second Activity"  
  28.         android:textSize="20sp" />  
  29.   
  30. </RelativeLayout>  

ActivityOne class

File: MainActivityOne.java
  1. package com.example.explicitintent2;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.view.Menu;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9. import android.widget.Toast;  
  10. public class ActivityOne extends Activity {  
  11.     /** Called when the activity is first created. */  
  12.       @Override  
  13.       public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.activity_main);  
  16.         Button button1=(Button)findViewById(R.id.Button01);  
  17.           
  18.         button1.setOnClickListener(new OnClickListener(){  
  19.          public void onClick(View view) {  
  20.           Intent i = new Intent(getApplicationContext(), ActivityTwo.class);  
  21.           i.putExtra("Value1""Android By Kundan");  
  22.           i.putExtra("Value2""Simple Tutorial");  
  23.           // Set the request code to any code you like, you can identify the  
  24.           // callback via this code  
  25.           startActivity(i);  
  26.          }  
  27.          });  
  28.       }  
  29.     }   

ActivityTwo class

File: MainActivityTwo.java
  1. package com.example.explicitintent2;  
  2. import android.app.Activity;  
  3. import android.content.Intent;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.EditText;  
  9. import android.widget.TextView;  
  10. import android.widget.Toast;  
  11. public class ActivityTwo extends Activity {  
  12. /** Called when the activity is first created. */  
  13.   @Override  
  14.   public void onCreate(Bundle bundle) {  
  15.     super.onCreate(bundle);  
  16.     TextView tv=new TextView(this);  
  17.     tv.setText("second activity");  
  18.     setContentView(R.layout.activity_two);  
  19.     Bundle extras = getIntent().getExtras();  
  20.     String value1 = extras.getString("Value1");  
  21.     String value2 = extras.getString("Value2");  
  22.     Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+  
  23.          "\n Second Value: "+value2,Toast.LENGTH_LONG).show();  
  24.     Button button1=(Button)findViewById(R.id.Button01);  
  25.     button1.setOnClickListener(new OnClickListener(){  
  26.         public void onClick(View view) {  
  27.             Intent i = new Intent(getApplicationContext(), ActivityOne.class);  
  28.             startActivity(i);  
  29.           }  
  30.     });  
  31.   }  
  32. }   

No comments:

Post a Comment