Tutorials Android How to Pass a Data from One Activity to Another in Android

How to Pass a Data from One Activity to Another in Android

In Android user interface is displayed through an activity. Activity is used to represent the data to user and allows user interaction. In an android application, we can have multiple activities and that can interact with each other. During activity interaction we might required to pass data from one activity to other. This tutorial I will explain more about, how to send data while switching between one Activity to another.

For example, if you are developing an application for RSS News feed reader, you may require having at least of two screens. While the first screen displaying the List of news feeds and other displaying the feed details. Below is the screenshot from the example.

pass a data from one Activity to another in Android

1. Calling one activity from another in android

We can call one activity from another by using Intents. Intent is one of the main building block which provides an abstract description of an operation to be performed. startActivity(intent) method belongs to your Activity class and can be used for starting a new activity.

Intent intent = new Intent(context, YourActivityClass.class);
startActivity(intent);

2. Passing data from one activity to other in android

An intent contains the action and optionally additional data. The data can be passed to other activity using intent putExtra() method. Data is passed as extras and are key/value pairs. The key is always a String. As value you can use the primitive data types int, float, chars, etc. We can also pass Parceable and Serializable objects from one activity to other.

Intent intent = new Intent(context, YourActivityClass.class);
intent.putExtra(KEY, <your value here>);
startActivity(intent);

3. Retrieving bundle data from android activity

You can retrieve the information using  getData() methods on the Intent object. The Intent object can be retrieved via the getIntent() method.

  Intent intent = getIntent();
  if (null != intent) {
	String stringData= intent.getStringExtra(KEY);
	int numberData = intent.getIntExtra(KEY, defaultValue);
	boolean booleanData = intent.getBooleanExtra(KEY, defaultValue);
	char charData = intent.getCharExtra(KEY, defaultValue);	
  }

To retrieve Serializable and Parcelable object using getSerializableExtra(KEY) and getParcelableExtra(KEY) method.

4. Example and case study

In this example, we will develop one list view of objects and clicking on a list row, it will open up another actiity displaying the detail of that row. Here, we are passing the row data from one activity to other.

4.1. Defining case study layouts

activity_news_list.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewsListActivity" >

    <ListView
        android:id="@+id/newsList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

</RelativeLayout>

snippet_news_list_row.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="80dp"
    android:orientation="vertical"
    android:minHeight="60dp"
     android:background="#99CC00"
    tools:context=".NewsListActivity" >

    <TextView
        android:id="@+id/newsHeadline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="News Headline"
        android:padding="5dp"
        android:minLines="2"
        android:maxLines="2"
        android:textStyle="bold"
        android:textColor="#0000dc"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/pubDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="19dp"
        android:text="Published Date"
        android:padding="3dp"
        android:textColor="#FF4444"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</LinearLayout>

activity_news_details.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".NewsListActivity" >

    <TextView
        android:id="@+id/headlines"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:padding="8dp"
        android:text="Headline"
        android:background="#99CC00"
        android:textStyle="bold"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#0000dc" />

    <TextView
        android:id="@+id/pub_date"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:gravity="right"
        android:padding="3dp"
        android:text="Published Date"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:background="#33B5E5"
        android:textColor="#0000dc" />

    <TextView
        android:id="@+id/description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:text="News Description"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</LinearLayout>

4.2. Defining Java Classes

NewsItem.java

package com.javatechig;

public class NewsItem  {

	private String headline;
	private String details;
	private String pubDate;

	public String getHeadline() {
		return headline;
	}

	public void setHeadline(String headline) {
		this.headline = headline;
	}

	public String getDetails() {
		return details;
	}

	public void setDetails(String details) {
		this.details = details;
	}

	public String getPubDate() {
		return pubDate;
	}

	public void setPubDate(String pubDate) {
		this.pubDate = pubDate;
	}

}

NewsListActivity.java

Here we render custom list view to show the news items. NewsItems are shown with alternative colors for each rows. When a list row is clicked, we pass the data to NewsDetailsActivity using intent extras.

package com.javatechig;

import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class NewsListActivity extends Activity {

	private NewsListAdapter adapter;

	public static final String KEY_HEADLINE="news_headline";
	public static final String KEY_DETAILS="news_details";
	public static final String KEY_PUBDATE = "news_pub_date";

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_list);

		adapter = new NewsListAdapter(this, getData());
		ListView list = (ListView) findViewById(R.id.newsList);
		list.setAdapter(adapter);

		list.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> adapterView, View view, int position, long offset) {
				NewsItem item = (NewsItem) adapter.getItem(position);

				Intent intent = new Intent(getApplicationContext(), NewsDetailsActivity.class);
				intent.putExtra(KEY_HEADLINE, item.getHeadline());
				intent.putExtra(KEY_PUBDATE, item.getPubDate());
				intent.putExtra(KEY_DETAILS, item.getDetails());

				startActivity(intent); 
			}
		});
	}

	private ArrayList<NewsItem> getData() {
		ArrayList<NewsItem> newsList = new ArrayList<NewsItem>();
		String[] headlines = getResources().getStringArray(R.array.news_headlines);
		String[] pubDate = getResources().getStringArray(R.array.news_pubdate);
		String[] details = getResources().getStringArray(R.array.news_details);

		for (int i = 0; i < headlines.length; i++) {
			NewsItem item = new NewsItem();
			item.setHeadline(headlines[i]);
			item.setPubDate(pubDate[i]);
			item.setDetails(details[i]);
			newsList.add(item);
		}
		return newsList;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

NewsListAdapter.java

This adapter will be used for custom list view. Here in this we are using alternative color for each of the list row.

package com.javatechig;

import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class NewsListAdapter extends BaseAdapter {

	private ArrayList<NewsItem> listData;
	private LayoutInflater layoutInflater;
	private Context context;

	public NewsListAdapter(Context context, ArrayList<NewsItem> listData) {
		this.listData = listData;
		this.context = context;
		layoutInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		return listData.size();
	}

	@Override
	public Object getItem(int position) {
		return listData.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		if (convertView == null) {
			convertView = layoutInflater.inflate(R.layout.snippet_news_list_row, null);
			holder = new ViewHolder();
			holder.headline = (TextView) convertView.findViewById(R.id.newsHeadline);
			holder.pubDate = (TextView) convertView.findViewById(R.id.pubDate);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.headline.setText(listData.get(position).getHeadline());
		holder.pubDate.setText(listData.get(position).getPubDate());

		if (position % 2 == 1) {
			convertView.setBackgroundColor(context.getResources().getColor(R.color.list_row_color1));  
		} else {
			convertView.setBackgroundColor(context.getResources().getColor(R.color.list_row_color2));  
		}

		return convertView;
	}

	static class ViewHolder {
		TextView headline;
		TextView pubDate;
	}

}

NewsDetailsActivity.java

In NewsDetailsActivity we are retrieving the data passed from NewsListActivity.

package com.javatechig;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.widget.TextView;
import static com.javatechig.NewsListActivity.KEY_HEADLINE;
import static com.javatechig.NewsListActivity.KEY_PUBDATE;
import static com.javatechig.NewsListActivity.KEY_DETAILS;

public class NewsDetailsActivity extends Activity {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_news_details);

		String headline = "";
		String details = "";
		String pubDate = "";

		Intent intent = getIntent();
		if (null != intent) {
			headline = intent.getStringExtra(KEY_HEADLINE);
			details = intent.getStringExtra(KEY_DETAILS);
			pubDate = intent.getStringExtra(KEY_PUBDATE);
		}

		TextView headlineTxt = (TextView) findViewById(R.id.headlines);
		headlineTxt.setText(headline);

		TextView pubdateTxt = (TextView) findViewById(R.id.pub_date);
		pubdateTxt.setText(pubDate);

		TextView descriptionTxt = (TextView) findViewById(R.id.description);
		descriptionTxt.setText(details);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}
}

5. Download Complete Example

Here you can download complete eclipse project source code from GitHub.

Total
0
Shares
guest
23 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Anita mail
Anita mail
8 years ago

Hello, Can you please help me how I am able to add different images to different titles in this list? how should I define the holder for ImageView?

Anita mail
Anita mail
8 years ago

Hello , Can you please tell me how I am able also put different images for different headlines? how can I add imageView to the NewsListAdapter?

javatechig
javatechig
8 years ago
Reply to  Anita mail
yiou
yiou
8 years ago

At 3, Is there a reason why you write null != intent instead of intent != null? or is it just a personal habit?

javatechig
javatechig
8 years ago
Reply to  yiou

No, there is no functional difference. It is just the way different people like differently.

javatechig
javatechig
7 years ago

Updated!

itechaleart
itechaleart
7 years ago

Wonderful article.. 🙂

Muhammed Hussain
Muhammed Hussain
7 years ago

Dear Sir i need this same tutorial from mysql db

JavaTechig
JavaTechig
7 years ago

Hi Do you mean from SQLite db?

Following data types you can pass between activities, you can pass primitive types such as int, string double serializable or parable object.

If you have bulk of data to be fetched from db. I would recommend just pass the key or id field and query from DB when required.

Find reference to SQLite tutorial here
http://stacktips.com/android/android-sqlite-database-tutorial

JavaTechig
JavaTechig
7 years ago

I don’t understand your question well.

Just a side note:
Android doesn’t support any direct way of connecting to MySQL. Mobile apps usually depends on REST or some other kind of web services for that.

However, android application supports SQLite. SQLite is a optimized database used in most mobile os. Checkout my Android SQLite database tutorial

Muhammed Hussain
Muhammed Hussain
7 years ago
Reply to  JavaTechig

Dear Sir. I need to use json. Only make from json. because the json page will fetch data from mysql db server.

JavaTechig
JavaTechig
7 years ago

Paste your code.. I don’t understand what you need

Nilanchala Panigrahy
Nilanchala Panigrahy
7 years ago

What is the problem?

Ajish Nair
Ajish Nair
7 years ago

Hello Nilanchala Sir, i have one doubt. Actually i want to develop the android app of 500 songs lyrics. So can you tell me from the above code where i will add the lyrics that is descriptions even where i want to add the listview as Songs 1, Songs 2 and so on. This app will work as when the user click on songs 1, the user will see the lyrics in another activity. Please help me out. Thanks in advance.

Ajish Nair
Ajish Nair
7 years ago

Hello Nilanchala Sir, i have one doubt. Actually i want to develop the android app of 500 songs lyrics. So can you tell me from the above code where i will add the lyrics that is descriptions even where i want to add the listview as Songs 1, Songs 2 and so on. This app will work as when the user click on songs 1, the user will see the lyrics in another activity. Please help me out. Thanks in advance.

vinod
vinod
6 years ago

sir, how to implement add to cart using recyclerview cardview by using php mysql in android

kadhar basha
kadhar basha
6 years ago

hi sir, the above tutorial, is it pass the .doc extension file data?

Nilanchala Panigrahy
Nilanchala Panigrahy
6 years ago
Reply to  kadhar basha

Intent bundle allows your to pass only key value or Parcelable type of data. For documents, images or larger data, you should consider using android fie system. Save the file in file system and access it back in Activity2 using the file path.

Nilanchala Panigrahy
Nilanchala Panigrahy
5 years ago

Image should not be passed as an activity bundle. You must have a caching mechanism or store it in internally and just pass the image path in bundle.

Jimmy
Jimmy
5 years ago

Hello
In android studio all work perfectly, but it’s not displaying image in listview, its show only “no_image(default image)” in place of image.

Neeraj soni
Neeraj soni
4 years ago

can i pass textdrawable in second activity

sangeetha
sangeetha
4 years ago

i have the doubt of how to pass current page data to cartlist page by clicking of addtocart button

sangeetha
sangeetha
4 years ago

anyone help me

Previous Post

How to create a HTTP adapter in IBM Worlight

Next Post
Starting an activity for a result

Starting an Android Activity for Result

Related Posts
By clicking “Allow All”, you agree to the storing of cookies on your device to enhance site navigation, analyze site usage, and assist in our marketing efforts. Cookie Notice
Allow All
23
0
Would love your thoughts, please comment.x
()
x