Passing arraylist in intent from one activity to another - android

I have a class 'Product' and i need to pass the arraylist of 'Product' objects from one activity to another.
I read that i can do so by making my Product class as Parcelable and use : putParcelableArrayListExtra (String name, ArrayList value).
In my app, i need to pass arraylists of objects from one activity to another. In this case, i need to make all those classes as parcelable and implement the interface methods.
Is this the correct way to solve the problem? Is there any other way to send the list of objects in intents?
Thanks..

Yes, the correct way is to use Parcelable or do the serialization to another kind of primitive object that you can put to Intent's extras

Related

How to transfer an ArrayList of objects from one Activity to another

I have an ArrayList of objects in one activity and I need this arrayList in another activity.
Is there a solution to transger this ArrayList? I know i can use intents for ArrayList of Strings but what's about the ArrayList of objects?
Thank you.
Complex types passed by means of Parcelable or do the serialization to another kind of primitive object that you can put to Intent's extras. see this question:
Help with passing ArrayList and parcelable Activity
and this tutorial:
Passing a list of objects between Activities

Parceable ArrayLists (Android)

Im trying to pass objects with intents between activities in an Android app. I know that objects must be made parceable or serializable (sorry for my spelling) before they can be passes through intents. However, in my case the objects are in an array list when they are passed through the intent.
Do I have to make the arraylist parceable before I pass it? Or do I simply have to make the objects in the list parceable when I pass it? Iv'e been having a lot of trouble passing theses array lists and would appreciate any help. Thanks.
Objects in ArrayList should be parcelable. Here is good example on how to do it.Android arraylist parcelable

Pass complex object though intent

I need to pass a list of cookies from one activity to the other using an Intent. How would one go about achieving this?
Thanks.
Using Intent.putExtra(String name, String[] value) would be the easiest way to pass a list of cookies. There are other putExtra signatures as well, depending on how you currently have your list implemented. On the other side, you would use getExtras() to get values. If for some reason, you had a more complex setup, you could create a Cookie class which extends Parcelable.
Just pass it along in the setExtra() method of the Intent class.
Hope it helped,
JQCorreia

Passing ArrayList with objects to new Activity?

I'm trying to pass an ArrayList from my first Activity to the next one. Basically, the first activity parses an XML file and creates an ArrayList with objects inside. What I want to do is send that ArrayList to my second activity and show some of the object data in a ListView.
I thought of doing this with an intent, but it looks like only primitive datatypes are usually passed through intents. Is this right?
If so, what would be a better solution to pass the data? Certainly Android must provide something to be able to do this kind of thing.
Any help/code examples are really appreciated..
Thanks
EDIT:
I solved this by first creating and calling the intent, and only parsing the XML in the Activity that I called. This way I didn't need to pass the objects anymore. But for those interested, you can read about how to pass data through activities, here.
Complex types may be passed by means of Parcelable. An example is in this question:
Help with passing ArrayList and parcelable Activity
I would do a toString on the array and pass it as an extra by doing a intent.putExtra("label". array.toString()); and then just recover it in the new activity.
You can pass a String List using putStringArrayListExtra(String name, ArrayList<String> value) if it is strings. Or, you could serialize List and then use putExtra(String name, Serializable value).
If you don't want to/can't use the above, you could use a central util class with a static reference to the List. Just set it in your first Activity and get it in the second.
This could be totally bad practice and I wouldn't know any better, but you could declare the ArrayList as public and static. Then just access it with Activity.ArraylistName.
You can set the scope of ArrayList at application level or you can do this using parcelable.
I made this trick to send from first Activity to second.
The first activity
ArrayList<String> mylist = new ArrayList<String>();
Intent intent = new Intent(ActivityName.this, Second.class);
intent.putStringArrayListExtra("key", mylist);
startActivity(intent);
The second activity
To retrieve
ArrayList<String> list = getIntent().getStringArrayListExtra("key");

how to save data in next activity

hi to all
I what to know how to save data in next activity
Use Intent to pass data from one Activity to another Activity.
But these Intents may carry native data types like String, int, float, double etc.
If you want your own Object to be passed, you would have to implement Serializable interface.
An odd way is to use a static Object in a class which is globally accessible.
you should explain clearly about your requirement. As i have understood, i think you want to access data which belongs to one activity in the other activity.
Refer the link below, you will get a clear idea.
http://developer.android.com/resources/faq/framework.html#3
u can use intents, bundles, or any other static variables to holds data from one activity to another

Categories

Resources