
Hey, what’s up, guys. Here I am with another post.
A single activity is not enough for my app? I want to use multiple activities in my app? If you are asking yourself these two questions and wondering how can I start an activity from another activity, then you are at the right place. In this post, we are going to see how can you start an activity from another activity. So, let’s begin.
To start an activity from another activity you need one thing and that thing is Intent.
You need an Intent class to start an activity from another activity.
Let’s see it with an example.
We will make a simple app with two activities in which one activity will start another activity.
activity_main.xml
<?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=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hi there"
android:textColor="#00ACC1"
android:textSize="36sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.283" />
<Button
android:id="@+id/button"
android:layout_width="145dp"
android:layout_height="55dp"
android:layout_marginTop="136dp"
android:text="Click Me"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.intentexample;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=findViewById(R.id.button);
//The first parameter of Intent is the activity
// which is starting another activity and
// the second parameter is the name of the
// activity you are starting.
//In this case, MainActivity.java is starting Activity2.java
Intent intent= new Intent(this, Activity2.class);
//Making button to respond by setting onClickListener
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View view) {
//Starting second activity(Acitivity2.java)
// by passing intent as a parameter.
startActivity(intent);
}
});
}
}
activity_2.xml
<?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=".Activity2">
<TextView
android:id="@+id/textView2"
android:layout_width="343dp"
android:layout_height="85dp"
android:layout_marginBottom="376dp"
android:text="you just learned how to start a new activity. "
android:textColor="#00897B"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Congratulations!"
android:textColor="#00897B"
android:textSize="36sp"
app:layout_constraintBottom_toTopOf="@+id/textView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.77" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity2.java
package com.example.intentexample;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class Activity2 extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
}
}
Here what we are doing is that we are creating Intent instance and passing two parameter to its constructor. The first parameter tells the intent which activity wants to start another activity and the second parameter is the target activity which we we want to start. In our case the first activity is MainAcitvity.java and the second activity is MainActivity2.java.


That’s it. You just learned how to start an activity from another activity.
Goodbye.
Leave a Reply