Cheap VPS & Xen Server


Residential Proxy Network - Hourly & Monthly Packages

Explicit Intent


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.     <Button
  7.         android:id=“@+id/Button01”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_below=“@+id/TextView01”
  12.         android:layout_marginLeft=“65dp”
  13.         android:layout_marginTop=“38dp”
  14.         android:onClick=“onClick”
  15.         android:text=“Call second activity” />
  16.     <TextView
  17.         android:id=“@+id/TextView01”
  18.         android:layout_width=“wrap_content”
  19.         android:layout_height=“wrap_content”
  20.         android:layout_alignLeft=“@+id/Button01”
  21.         android:layout_alignParentTop=“true”
  22.         android:layout_marginLeft=“18dp”
  23.         android:layout_marginTop=“27dp”
  24.         android:minHeight=“60dip”
  25.         android:text=“First Activity”
  26.         android:textSize=“20sp” />
  27. </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.     <Button
  7.         android:id=“@+id/Button01”
  8.         android:layout_width=“wrap_content”
  9.         android:layout_height=“wrap_content”
  10.         android:layout_alignParentLeft=“true”
  11.         android:layout_below=“@+id/TextView01”
  12.         android:layout_marginLeft=“65dp”
  13.         android:layout_marginTop=“38dp”
  14.         android:onClick=“onClick”
  15.         android:text=“Call First activity” />
  16.     <TextView
  17.         android:id=“@+id/TextView01”
  18.         android:layout_width=“wrap_content”
  19.         android:layout_height=“wrap_content”
  20.         android:layout_alignLeft=“@+id/Button01”
  21.         android:layout_alignParentTop=“true”
  22.         android:layout_marginLeft=“18dp”
  23.         android:layout_marginTop=“27dp”
  24.         android:minHeight=“60dip”
  25.         android:text=“Second Activity”
  26.         android:textSize=“20sp” />
  27. </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.         button1.setOnClickListener(new OnClickListener(){
  18.          public void onClick(View view) {
  19.           Intent i = new Intent(getApplicationContext(), ActivityTwo.class);
  20.           i.putExtra(“Value1”“Android By Kreationnext”);
  21.           i.putExtra(“Value2”“Simple Tutorial”);
  22.           // Set the request code to any code you like, you can identify the
  23.           // callback via this code
  24.           startActivity(i);
  25.          }
  26.          });
  27.       }
  28.     }

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. }

download this android example

Output:

explicitintentoutput1

explicitintentoutput2

 

Comments

comments