Start an Activity with a parameter
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Starting an activity with parameters is an essential concept in Android app development. It enables app developers to pass data from one activity to another, which is critical for creating interactive and dynamic applications. In Android, activities serve as the entry points for user interactions, and they often need to communicate to offer a seamless user experience. This article explores various methods to start an activity with a parameter, provides technical explanations, and presents illustrative examples.
Basics of Android Activities
Before jumping into how to pass data between activities, let's briefly understand what an activity is in Android:
- Activity: An activity is a single, focused thing that the user can do. It is a crucial component in Android development and serves as the primary building block of the application's user interface. Each activity is implemented as a subclass of the
Activityclass.
Passing Parameters
Between Activities
When developing Android applications, it is common to start a new activity while simultaneously passing some data to it. This is often necessary for passing information that the new activity requires to function or display relevant information to the user.
Intent Object
An Intent
in Android is an object that provides a runtime binding between separate components like two activities. It is mainly used to start activities and services. An Intent
can hold data in key-value pairs, where the key is always a string
.
Using putExtra()
Method
To pass parameters to an activity, we use the putExtra()
method of the Intent
class. Below is a simple example illustrating how to start an activity with a parameter.
- Primitive Data Types: Such as
int,float,double,Boolean. - Arrays: Including arrays of primitive data types and strings.
- Parcelable and Serializable: For complex objects, the class can either implement
ParcelableorSerializable. - Data Size: Intents have a limit on the amount of data that can be passed. Large datasets should be handled with databases or files.
- Security: Sensitive data should be encrypted when using Intents to mitigate risk exposure.
- Parcelable vs Serializable:
Parcelableis generally preferred in Android due to its performance benefits compared toSerializable.
Related reading
- START_STICKY and START_NOT_STICKY
- startForeground fail after upgrade to Android 8.1
- Starting iPhone app development in Linux?
- Static table view outside UITableViewController
- Static vs class functions/variables in Swift classes?
- Static way to get 'Context' in Android?
- Static way to get 'Context' in Android?
- Status bar and navigation bar appear over my view's bounds in iOS 7
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.