Pages Navigation Menu

Coding is much easier than you think

How do I pass data between activities in Android

How do I pass data between activities in Android

You may have persistent data such as session id to be used throughout the application in many activities. You may do this using any one of the following methods:
** UPDATE: Android Complete tutorial now available here.
1. In the First Activity where the common value is first initiated, create a new Intent as below:

Intent i = new Intent(getApplicationContext(), NewActivity.class);


You are adding the session value in the extras of the intent.

Then in the next coming Activities, retrieve those values:

Bundle extras = getIntent().getExtras();

if (extras != null) {

String value = extras.getString("session_id");


This is the easiest way.
2. You can create a Helper class and store the Common Values in it.

Create simple “helper” class(factory for your Intents), like this:

import android.content.Intent;
public class IntentManager {
public static final Intent Helper1(Intent src) {
  return new Intent("Helper1").addCategory("YourSpecialCategory").putExtras(src);

This will be the factory for all your Intents. Every time you need a new Intent, create static factory method in IntentManager. To create new Intent for the Helper class you should just use this line in your activity.:


When you want to “save” some data in “session” just use following:
In target Activity you can get the field value anytime as:


3. You can also use the SharedPreferences class provides a general framework that allows you to save and retrieve persistent key-value pairs of primitive data types.
You can use Shared Preferences to save any primitive data: booleans, floats, ints, longs, and strings. This data will persist across user sessions (even if your application is killed).
You will use getSharedPreferences() to get the SharedPreferences value and SharedPreferences.Editor editor = SharedPreferencesName.edit(); to store the value.
Refer to SharedPreferences tutorials for further reference.

About Gokul

I am very much interested in android and Wicket framework. A core android developer and working in android native app development and responsive web mobile design. I have also worked in wicket fame work and java web development. I will keep on updating you about android and wicket framework and answer your query.

%d bloggers like this: