Saturday, March 13, 2021

Android Explicit Intent

 

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. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.explicitintent.FirstActivity">  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginEnd="8dp"  
  13.         android:layout_marginStart="8dp"  
  14.         android:layout_marginTop="8dp"  
  15.         android:text="First Activity"  
  16.         app:layout_constraintBottom_toBottomOf="parent"  
  17.         app:layout_constraintEnd_toEndOf="parent"  
  18.         app:layout_constraintHorizontal_bias="0.454"  
  19.         app:layout_constraintLeft_toLeftOf="parent"  
  20.         app:layout_constraintRight_toRightOf="parent"  
  21.         app:layout_constraintStart_toStartOf="parent"  
  22.         app:layout_constraintTop_toTopOf="parent"  
  23.         app:layout_constraintVertical_bias="0.06" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/button"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_marginEnd="8dp"  
  30.         android:layout_marginStart="8dp"  
  31.         android:layout_marginTop="392dp"  
  32.         android:onClick="callSecondActivity"  
  33.         android:text="Call second activity"  
  34.         app:layout_constraintEnd_toEndOf="parent"  
  35.         app:layout_constraintStart_toStartOf="parent"  
  36.         app:layout_constraintTop_toTopOf="parent" />  
  37.   
  38. </android.support.constraint.ConstraintLayout>  

ActivityOne class

File: MainActivityOne.java
  1. package example.javatpoint.com.explicitintent;  
  2.   
  3. import android.content.Intent;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7.   
  8. public class FirstActivity extends AppCompatActivity {  
  9.   
  10.     @Override  
  11.     protected void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.         setContentView(R.layout.activity_first);  
  14.     }  
  15.     public void callSecondActivity(View view){  
  16.         Intent i = new Intent(getApplicationContext(), SecondActivity.class);  
  17.         i.putExtra("Value1""Android By Javatpoint");  
  18.         i.putExtra("Value2""Simple Tutorial");  
  19.         // Set the request code to any code you like, you can identify the  
  20.         // callback via this code  
  21.         startActivity(i);  
  22.     }  
  23.   
  24. }  

activitytwo_main.xml

File: activitytwo_main.xml
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="example.javatpoint.com.explicitintent.SecondActivity">  
  8.   
  9.     <TextView  
  10.         android:layout_width="wrap_content"  
  11.         android:layout_height="wrap_content"  
  12.         android:layout_marginEnd="8dp"  
  13.         android:layout_marginStart="8dp"  
  14.         android:layout_marginTop="8dp"  
  15.         android:text="Second Activity"  
  16.         app:layout_constraintBottom_toBottomOf="parent"  
  17.         app:layout_constraintEnd_toEndOf="parent"  
  18.         app:layout_constraintHorizontal_bias="0.454"  
  19.         app:layout_constraintLeft_toLeftOf="parent"  
  20.         app:layout_constraintRight_toRightOf="parent"  
  21.         app:layout_constraintStart_toStartOf="parent"  
  22.         app:layout_constraintTop_toTopOf="parent"  
  23.         app:layout_constraintVertical_bias="0.06" />  
  24.   
  25.     <Button  
  26.         android:id="@+id/button"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_marginEnd="8dp"  
  30.         android:layout_marginStart="8dp"  
  31.         android:layout_marginTop="392dp"  
  32.         android:onClick="callFirstActivity"  
  33.         android:text="Call first activity"  
  34.         app:layout_constraintEnd_toEndOf="parent"  
  35.         app:layout_constraintStart_toStartOf="parent"  
  36.         app:layout_constraintTop_toTopOf="parent" />  
  37. </android.support.constraint.ConstraintLayout>  

ActivityTwo class

File: MainActivityTwo.java
  1. package example.javatpoint.com.explicitintent;  
  2.   
  3. import android.content.Intent;  
  4. import android.support.v7.app.AppCompatActivity;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.widget.Toast;  
  8.   
  9. public class SecondActivity extends AppCompatActivity {  
  10.   
  11.     @Override  
  12.     protected void onCreate(Bundle savedInstanceState) {  
  13.         super.onCreate(savedInstanceState);  
  14.         setContentView(R.layout.activity_second);  
  15.         Bundle extras = getIntent().getExtras();  
  16.         String value1 = extras.getString("Value1");  
  17.         String value2 = extras.getString("Value2");  
  18.         Toast.makeText(getApplicationContext(),"Values are:\n First value: "+value1+  
  19.                 "\n Second Value: "+value2, Toast.LENGTH_LONG).show();  
  20.     }  
  21.     public void callFirstActivity(View view){  
  22.         Intent i = new Intent(getApplicationContext(), FirstActivity.class);  
  23.         startActivity(i);  
  24.     }  
  25.   
  26. }  

Output:

android explicit intent example output 1 android explicit intent example output 2 android explicit intent example output 3

No comments:

Post a Comment

Inapp update

  Inapp update https://desk.zoho.com/portal/vegabirdtech/en/kb/articles/how-to-use-burp-suite-with-android-mobile