Pass complex object though intent - android

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

Related

How to start Activity with custom parameters

I need to start an Activity with parameters. I know i can do it with Intents and Bundles, but as far as i know it's only possible with String, boolean, etc...
I have to do it with custom parameters, like user-made classes.
For example, i got PLC-class in main activity, which contains many variables and methods, and i need to use it in the other activity.
I know the "public static" method, but i'm not a fan of it, and i'm sure i can find something else that fits my need.
I hope you guys can help me
Bye
Or you can make your class implementing parcelable interface, and put it in a parcel that you send from your source activity to destination activity..
In my opinion you can make bean class... which have getter/setter method.. you can set value there, and retrieve those values in any other Activities.Using it class.
E.g
public class Constants{
public static Bean userBeen=new Bean();
}
Main activity
Constants.userBeen.setValue("anything");
In other Activity,, you can get value using userBeenobj;
Other Activity
String s=Constants.userBeen.getValue();
Hope this helps to you. I mostly use this.!
make your custom made class serializable and then put it in bundle like this
bundle.putSerializable(key, value);

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

Android and passing nested ArrayLists via Bundles

How do you pass an instance of ArrayList<ArrayList<HashMap<String, String>>> from one Android activity to another via an instance of Bundle?
(I could use JSON strings. I'd like to know if there are better ways.)
Thanking you kindly in advance.
You can pass it as an extra in the Intent that you use to fire up the new activity. Since ArrayList implements Serializable, you don't have to do anything special to feed it to Intent.putExtra().
In general it is not good to pass too many or too large data between activities via Intents. It's better to store them somewhere centrally and pass a lightweight identifier or something like this, so the other activity can retrieve them from the store.
E.g. you can use an Application class to store these data. An application class is always available as long as you application is running. You get it from each Activity by calling the getApplication() method.

Passing arraylist in intent from one activity to another

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

Categories

Resources