How Do I Transfer Data Between Android Activities?

Android, Android Transfer

Transferring data between Android activities is a fundamental concept in Android app development. Whether you want to pass user information, settings, or any other data from one activity to another, there are several methods you can use to achieve this.

Using Intent.putExtra()

The most common and straightforward way to transfer data between activities is by using the putExtra() method provided by the Intent class. This method allows you to add extra data to an intent, which can then be retrieved by the receiving activity.

To transfer data using this method, follow these steps:

  1. Create an instance of the Intent class:
  2. Intent intent = new Intent(MainActivity.this, SecondActivity.class);
  3. Add the data using the putExtra() method:
  4. intent.putExtra("key", value);
  5. Start the second activity:
  6. startActivity(intent);

In the receiving activity, you can retrieve the data using the getIntent() method and then extract the extra data:

Intent intent = getIntent();
String value = intent.getStringExtra("key");

Passing Objects Using Parcelable or Serializable

If you need to pass complex objects between activities, such as custom classes or lists, you can make your objects implement either the Parcelable or Serializable interface.

The Parcelable interface provides a high-performance option for passing objects between activities. To use it, follow these steps:

  1. Implement the Parcelable interface in your custom class:
  2. public class CustomObject implements Parcelable {
        // Implementation
  3. Implement the required methods:
  4. @Override
    public void writeToParcel(Parcel dest, int flags) {
        // Write object data to parcel
    public int describeContents() {
        return 0;
  5. In the sending activity, add the custom object to the intent:
  6. CustomObject customObject = new CustomObject();
    intent.putExtra("key", customObject);
  7. In the receiving activity, retrieve the object from the intent:
  8. CustomObject customObject = getIntent().getParcelableExtra("key");

The Serializable interface is a simpler alternative to Parcelable. It does not require any additional methods to be implemented in your custom class but may have slightly lower performance. To use it, follow similar steps as above but implement Serializable instead of Parcelable.

Using Application Class

If you need to share data across multiple activities or throughout your entire app, you can consider using the Application class. The Application class is a base class for maintaining global application state and can be accessed from any activity within your app.

To store and retrieve data using the Application class, follow these steps:

  1. Create a subclass of Application (if one doesn’t already exist):
  2. public class MyApp extends Application {
        // Implementation
  3. Define a variable in your Application class:
  4. public class MyApp extends Application {
        private String data;
        // Getter and Setter methods
  5. In your AndroidManifest.xml file, add the name attribute to specify your custom Application class:
  6. <application
  7. Access the data from any activity using the getApplication() method:
  8. MyApp myApp = (MyApp) getApplication();
    String data = myApp.getData();

These are some of the common methods to transfer data between Android activities. Choose the method that best suits your needs based on the complexity and type of data you want to transfer.

By mastering these techniques, you can easily pass data between activities in your Android app and enhance the overall user experience.