TechTorch

Location:HOME > Technology > content

Technology

Starting Another Activity from the Current Activity in Android

February 28, 2025Technology4405
Starting Another Activity from the Current Activity in Android In Andr

Starting Another Activity from the Current Activity in Android

In Android development, you often need to launch one activity from another. This process is commonly done using an Intent. This guide will walk you through the steps to achieve this, complete with code examples and a detailed explanation of the process.

Prerequisites

To follow along, you should be familiar with the following concepts:

Basic understanding of Android activity lifecycle and anatomy Android development environment (such as Android Studio) Basic knowledge of XML and Java (or Kotlin)

Step-by-Step Guide to Launching an Activity

1. Creating the Target Activity

The first step is to define the activity that you wish to start (i.e., the target activity or 'SecondActivity'). Here is how you can create it:

package 
import android.os.Bundle
import 
public class SecondActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(_second);
    }
}

Note the following:

The package name should match your project structure. Ensure that the SecondActivity class extends AppCompatActivity. The onCreate() method initializes the activity's user interface by setting the content view.

2. Starting the Activity from the First Activity

In the first activity (e.g., MainActivity), you'll need to create a button that triggers the launch of SecondActivity. Here’s how to do it:

package 
import android.os.Bundle
import 
import android.widget.Button
import 
import 
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(_main);
        Button button  findViewById(_start_second_activity);
        (new View.OnClickListener {
            @Override
            public void onClick(View v) {
                Intent intent  new Intent(, );
                startActivity(intent);
            }
        });
    }
}

Key points:

The () method sets an event listener to handle the button click. The Intent object is used to start the new activity. The first parameter is the current activity context, and the second parameter is the class of the target activity.

3. Updating the AndroidManifest.xml

To inform the Android system about the SecondActivity, you need to add its details to the AndroidManifest.xml file:

activity android:name""/

This line declares the SecondActivity to the Android system, making it known for deployment.

4. Passing Data (Optional)

If you wish to pass data from one activity to another, you can use the Intent object to include extra data:

Intent intent  new Intent(, );
intent.putExtra("key", "value");
startActivity(intent);

It is important to retrieve the data in the receiving activity using:

String value  getIntent().getStringExtra("key");

Summary

This guide has covered the basics of launching one activity from another in Android. By using Intent, you can easily connect different parts of your application. Customizations and additional features can be added based on your project's needs. If you have any additional questions or requirements, feel free to explore further or consult resource materials.