Android Development
Data Transfer
Mobile App Coding
Android Activities
Intent Extras

How do I pass data between Activities in Android application?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Passing data between activities is a fundamental aspect of building an Android application. Activities are a crucial component of any Android app, and they represent a single screen with a user interface. In many cases, one activity needs to pass data to another activity. Android provides several methods to do this, including using Intents, Bundles, SharedPreferences, and Singleton class.

Using Intents and Bundles

Intents are the primary means of passing data between activities. They facilitate communication between components in an Android application. Using Bundles, which are generally used to pack data into the Intent, makes the Intent capable of carrying relatively larger sets of data.

Example Code: Passing Data Using an Intent

java
Intent intent = new Intent(CurrentActivity.this, NextActivity.class);
intent.putExtra("KEY", "value");
startActivity(intent);

In NextActivity, you retrieve the data:

java
1Bundle extras = getIntent().getExtras();
2if (extras != null) {
3    String value = extras.getString("KEY");
4    // Use the value
5}

SharedPreferences

SharedPreferences provides a way to save data in key-value pairs. This approach is useful if you need to maintain the data across various activities or after closing and reopening the app.

Example Code: Using SharedPreferences

To save data:

java
1SharedPreferences sharedPreferences = getSharedPreferences("MySharedPref", MODE_PRIVATE);
2SharedPreferences.Editor myEdit = sharedPreferences.edit();
3myEdit.putString("KEY", "value");
4myEdit.apply();

To retrieve data in another activity:

java
SharedPreferences sh = getSharedPreferences("MySharedPref", MODE_PRIVATE);
String s1 = sh.getString("KEY", "");

Singleton class

A Singleton is a class that allows only one instance to be created and provides a global point of access to that instance. It can be used to hold data that needs to be shared across multiple components of an application.

Example of a Singleton Class

java
1public class DataHolder {
2    private static DataHolder instance = new DataHolder();
3    private String data;
4
5    public static DataHolder getInstance() {
6        return instance;
7    }
8
9    public void setData(String data) {
10        this.data = data;
11    }
12
13    public String getData() {
14        return data;
15    }
16}

Set data in one activity:

java
DataHolder.getInstance().setData("This is the data");

Retrieve data in another activity:

java
String data = DataHolder.getInstance().getData();

Table: Comparison of Methods

MethodType of DataPersistenceEase of Use
Intents and BundlesAny DataTransientEasy
SharedPreferencesKey-ValuePersistentModerate
Singleton classAny DataTransientModerate

Additional Considerations

  • Security and Privacy: When passing sensitive information between activities, consider the security implications. For instance, using Intents exposes data in the system log, making it vulnerable to interception by malicious apps.
  • Memory Management: Using Singleton for large amounts of data or complex data structures can lead to high memory usage and potential memory leaks. Manage the lifecycle of Singleton objects carefully.
  • Testing and Maintenance: Code involving data passing should be modular and abstract to facilitate easier testing and maintenance.
  • Data Type Support: Remember that different data passing techniques support different types of data. Intents and Bundles support basic data types and Parcelable objects, whereas SharedPreferences supports basic data types stored as strings or sets of strings.

In conclusion, while there are multiple methods to pass data between activities in an Android app, the choice of method depends significantly on the specific needs of the application, such as the type of data, the required persistence level, and security considerations. For most transient, simple data passing needs, Intents combined with Bundles are sufficient. For small amounts of persistent data, SharedPreferences is more suitable. For global access to data throughout the application lifecycle without persistent storage, a Singleton class might be the most efficient approach.


Course illustration
Course illustration

All Rights Reserved.