How to pass an arraylist value from one activity to another in android?

This simple example shows how to pass an ArrayList from one activity to another in Android.
This is the first activity on which we are sending the arrayList.

package com.coderzheaven.passarraylist;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final ArrayList<String> arr = new ArrayList<String>();
        arr.add("Hello");
        arr.add("CoderzHeaven");
        
        findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(MainActivity.this, Second.class);
				intent.putExtra("array_list", arr);
				startActivity(intent);
				
			}
		});
        
        
    }
}

Now this is the second activity in which we are receiving the passed arrayList.

package com.coderzheaven.passarraylist;

import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;

public class Second extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);
        
        Bundle b = getIntent().getExtras();
        
        if(b!=null){
        	ArrayList<String> arr = (ArrayList<String>)b.getStringArrayList("array_list");
        	System.out.println(arr);
        }    
     
    }
}

This is the AndroidManifest file.

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.coderzheaven.passarraylist"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        
        <activity android:name=".Second"></activity>
    </application>

</manifest>

Please check the LogCat for the Output.

Download complete tutorial PDF from below.

Pass ArrayList Tutorial PDF

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>