Android-Intent
An intent is a message object used by Android that enables interaction and communication between various parts of one application or between applications. It is used to communicate requests for actions or information amongst different parts, including activities, services, and broadcast receivers.
There are two types of intent:
- Implicit
- Explicit
Implicit Intent: The component is not specifically mentioned by Implicit Intent. In such a situation, intent gives information about the components that the system that is to be invoked has made available.
Here is an example of Implicit intent that displays a webpage,
First write this code in main_activity.xml,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/editText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="60dp" android:ems="10" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="8dp" android:layout_marginLeft="156dp" android:layout_marginTop="172dp" android:text="Submit" /> |
Now put the code in MainActivity.java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
package com.example.layouts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { Button button; EditText editText; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); button = findViewById(R.id.button); editText = findViewById(R.id.editText); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String url=editText.getText().toString(); Intent intent=new Intent(Intent.ACTION_VIEW, Uri.parse(url)); startActivity(intent); } }); } } |
Explicit Intent: Android’s explicit intent defines the component that will be called from an activity. In other words, we can explicitly call one activity to another in Android. We can also transfer data from one activity to another using explicit intent.
Here is an example of explicit intent,
- First, write this code in activity_main.xml which is default activity,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="First Activity" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="392dp" android:onClick="callSecondActivity" android:text="Call second activity" /> </LinearLayout> |
2.Now write this in MainActivity.java,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
package com.example.layouts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void callSecondActivity(View view){ Intent i = new Intent(getApplicationContext(), SecondActivity.class); i.putExtra("Value1", "This is for one"); i.putExtra("Value2", "This is for second"); startActivity(i); } } |
3. Now create a second empty activity name as SecondActivity and write the code under activity_second.xml,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" tools:context=".SecondActivity"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="8dp" android:text="Second Activity" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginEnd="8dp" android:layout_marginStart="8dp" android:layout_marginTop="392dp" android:onClick="callFirstActivity" android:text="Call first activity" /> </LinearLayout> |
Now write these lines under the SecondActivity.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
package com.example.layouts; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Toast; public class SecondActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); 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(); } public void callFirstActivity(View view){ Intent i = new Intent(getApplicationContext(), MainActivity.class); startActivity(i); } } |