Sendind an Extra with my own kind of data - android

I need to pass an extra data to an activity, but I don't know how since my data is a variable from a specific class of mine. I have something like:
MyClass variable=new MyClass();
Intent intent= new Intent(this,SecondActivity.class);
intent.putExtra("variableName", variable);
I know the error in code is obvious, but I want to know how can I send data from a class of mine.

Please looking into Parcelable
How can I make my custom objects Parcelable?
http://developer.android.com/reference/android/os/Parcelable.html

You will have to serialize the data and then pass. Android doesn't support passing custom objects as extras. Look at this answer here. Passing custom objects between activities?

Related

Is it possible to pass Stack, Queue or Hashtable to an Intent?

We can pass data between activity. For example, getStringExtra, getIntExtra and getArraylistExtra on intent. Is there any way to pass more complex data structured such as Stack, Queue, Hashtable, etc? If so, how?
If the elements inside your stack or queue are implementing Serializable, then you can.
Send data with:
Intent intent=new Intent(context, BlahActivity.class);
intent.putExtra("data", (Serializable)data);
startActivity(intent);
Where data is your queue/stack, and extract with:
Intent intent=getIntent();
received_data=(<YOUR_COMPLEX_DATA_TYPE>)intent.getSerializableExtra("data");
As long as your element types implement Serializable then you can use getSerializableExtra().
The easiest way would be to put them into a List or ArrayList, then serialize with Json.NET/ServiceStack/ProtoBuf into string, put that string into the intent and on the other side deserialize it back into List.
If the activity is only meant to run from your code only, I usually just use a static Dictionary container and then push/pull with the Guid going into the string extras. This is a container for ViewModels: https://github.com/sami1971/SimplyMobile/blob/master/Core/SimplyMobile.Core/ViewModelContainer.cs
Extensions and using ViewModel approach help with pulling the data:
https://github.com/sami1971/SimplyMobile/blob/master/Android/SimplyMobile.Android/Extensions/ActivityExtensions.cs

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");

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

Transport own class with Intent between objects

HI,
i'm programming an android application using the StartActivityForResult() - Method.
But, i want to get as result an own class, not a string or something like that.
how can i tell the intent to use the class i wrote to give back?
You have to have your class implement Parcelable or Serializable. Also, this technique is only recommended for activities within the same application.

Categories

Resources