Android development
passing parameters
activity lifecycle
Android intents
app development

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.

Browse interview questions

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 Activity class.

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 Parcelable or Serializable .
  • 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: Parcelable is generally preferred in Android due to its performance benefits compared to Serializable .

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.