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
In NextActivity, you retrieve the data:
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:
To retrieve data in another activity:
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
Set data in one activity:
Retrieve data in another activity:
Table: Comparison of Methods
| Method | Type of Data | Persistence | Ease of Use |
| Intents and Bundles | Any Data | Transient | Easy |
| SharedPreferences | Key-Value | Persistent | Moderate |
| Singleton class | Any Data | Transient | Moderate |
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.

