Android Intents

Naved shaikh
2 min readJan 1, 2021

Android Intent is a message that is passed between components such as activity, services, content provider, broadcast receiver etc.

it is generally used with startActivity() method to invoke activity, broadcast receiver etc.

The dictionary meaning of Intent is intention or purpose, so it can be described as intention to do action.

Android Intents are mainly use to

  • Start a Activity
  • Start a Service
  • Display a web page
  • Broadcast a message
  • Dial phone call etc.

Types of Android Intents

Intent are classified in two types Implicit and Explicit.

Implicit Intent doesn’t specify the component. In such case Intent provides the information of the available components provide by the system that is to be invoked.

For example : code to view a webpage

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(“http://www.google.com"));
startActivity(intent);

Explicit Intent specify the component. In such case Intent provides the external class that is to be invoked.

For example : code to start an activity

Intent intent = new Intent(this, ActivityB.class);
startActivity(intent);

You can also pass the information from one activity to another using Explicit Intent.

Android Explicit Intent Example

Lets see an example of calling one activity to another and versa

activity_first.xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Activiyt!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

FirstActivity.java class file

public class FirstActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

goToSecondActivity();
}

private void goToSecondActivity() {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
}

activity_second.xml file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SecondActivity">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Activiyt!"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

SecondActivity.java class file

public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);

goToFirstActivity();
}

private void goToFirstActivity() {
Intent intent = new Intent(this, FirstActivity.class);
startActivity(intent);
}
}

--

--