---
title: "How can you pass data between activities in Android?"  
description: "How can you pass data between activities in Android?"  
author: "Steilla Mitchel"  
published: 2023-06-27  
updated: 2023-06-28  
canonical: https://www.mindstick.com/forum/158881/how-can-you-pass-data-between-activities-in-android  
category: "android"  
tags: ["android", "android activity"]  
reading_time: 2 minutes  

---

# How can you pass data between activities in Android?

How can you [pass data](https://www.mindstick.com/forum/351/how-to-pass-data-from-controller-to-ascx-page) between [activities](https://www.mindstick.com/interview/2589/describe-android-activities-in-brief) in [Android](https://www.mindstick.com/articles/13046/10-reasons-why-i-prefer-android-vs-iphone-ios)?

## Replies

### Reply by Aryan Kumar

There are two ways to pass [data](https://www.mindstick.com/articles/13050/salesforce-aiming-to-dominate-predictive-analytics-with-data-science) between activities in Android:

1. **Using intents:** Intents are objects that are used to start activities, services, and broadcast receivers. They can also be used to pass data between activities. To pass data using intents, you need to use the `putExtra()` method to add the data to the intent. The data can be of any type, such as strings, integers, or objects. When the activity is started, the data will be passed to the activity's `onCreate()` method.
2. **Using shared preferences:** Shared preferences are a way to store small amounts of data in a file on the device. They can be used to store data that needs to be persisted across app sessions, such as user settings or login information. To pass data using shared preferences, you need to store the data in the shared preferences file in the first activity. Then, in the second activity, you can retrieve the data from the shared preferences file.

Here is an example of how to pass data using intents:

Code snippet

```plaintext
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("name", "John Doe");
startActivity(intent);
```

In this example, we are creating an intent to start the `SecondActivity` class. We are also adding a string with the key "name" and the value "John Doe" to the intent. When the `SecondActivity` is started, the data will be passed to the activity's `onCreate()` method.

Here is an example of how to pass data using shared preferences:

Code snippet

```plaintext
SharedPreferences sharedPreferences = getSharedPreferences("my_preferences", Context.MODE_PRIVATE);
String name = sharedPreferences.getString("name", "");
```

In this example, we are getting the data from the shared preferences file. The file is named "my_preferences" and it is stored in the private mode. The key of the data is "name" and the default value is an empty string.

Which method you use to pass data between activities will depend on your specific needs. If you need to pass a small amount of data, then using intents is a good option. If you need to pass a larger amount of data or if you need to persist the data across app sessions, then using shared preferences is a good option.


---

Original Source: https://www.mindstick.com/forum/158881/how-can-you-pass-data-between-activities-in-android

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
