0

This is on the android platform, i have a list of objects of type (FitItem), and i want pass the list from my activity A to another activity B, and on the activity B i want get the list of objects again

shontauro
  • 1,812
  • 1
  • 25
  • 26

5 Answers5

2
Intent yourIntent = new Intent(activityA.this, activityB.class);
Bundle yourBundle = new Bundle();
yourBundle.putString("name", value);
yourIntent.putExtras(yourBundle);
startActivity(yourIntent);

And you get the value in the next Activity (in your onCreate()):

Bundle yourBundle = getIntent().getExtras();
String s = yourBundle.getString("name");

This example is passing a String, but you should be able to grasp how to use it for other objects.

Cat
  • 66,919
  • 24
  • 133
  • 141
bschultz
  • 4,244
  • 1
  • 25
  • 33
1

You must serialize your object.

"Seriawhat?"
Ok, first things first: What is object serialization?

"Got it, but how do I do that?"
You can use Parcelable (more code, but faster) or Serializable (less code, but slower).
Parcelable vs Serializable.

"Save me some time, show me the code!"
Ok, if you'll use Parcelable, see this.

And if you'll use Serializable, see this and/or this.

Hope that helps!

Heitor Colangelo
  • 494
  • 9
  • 16
1

For custom classes: You will have to have your FitItem class implements Parcelable.

Then in Activity A, from an Intent object, use putParcelableArrayListExtra to pass the list of FitItem to Activity B and in your Activity B, use getParcelableArrayListExtra to retrieve the list of FitItem

If you want to pass list of String, Integer, Float ..., refer to bschultz post

ccheneson
  • 49,072
  • 8
  • 63
  • 68
0

Implements Serializable in model class. Then you can pass model class using bundle/intent.

Dhara Jani
  • 461
  • 3
  • 10
0

If they're if the object is serializable, just add them as extras to the intent. I think something like this:

// in Activity A, when starting Activity B
Intent i = new Intent(this, ActivityB.class);
i.putExra("fitItemList", fitItemList);
startActivity(i);

// in Activity B (onCreate)
ArrayList<FitItem> fitItemList = savedInstanceState.getSerializableExtra("fitItemList");

edit: Just like bschultz already posted :)

Krylez
  • 17,414
  • 4
  • 32
  • 41