How to pass a List using Intent from an Activity to or Activity

Asked

Viewed 954 times

0

I need to pass List<Carro> carro using Intent of a Activity to or Activity.

Car is a class with several elements String modelo, String categoria Double potencia

1 answer

1

First, make your class implement Serializable, example:

public class Carro implements Serializable{}

In your Internet you cast a serializable:

List<Carro> list = new ArrayList<Carro>();
myIntent.putExtra("LIST", (Serializable) list);

In the second Activity you can get the list like this:

Intent i = getIntent();
list = (List<Carro>) i.getSerializableExtra("LIST");

There are more performatic ways to pass parameters between acitivities using Parcelable.

  • And extremely recommended to use Parcelable, since a list can be great, consider using an Asynctask to run putExtra or getSerializableExtra.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.