I have an object that has (among other things) a list of Intents. I want to pass this object as an extra to an Intent. However, the Intent class is not serializable, it is just "Parcelable".
I assume that Parcelable is the android version of Serializable, but I'd rather not have to write my own serialization code for my class, and Parcelable seems to require that.
Anyone have any solutions to this other than just reimplementing the Intent as a serializable class?
You can put a Parcelable in an Intent extra, and an Intent is already Parcelable. All you need to do is make your object Parcelable and you are set.
Another Route you can take:
make your list of intents transient(not included in a serializable),
then pass it as a parcelable array via Intent.putExtra(String key, Parcelable[] value).
The receiving class can then recreate your Object.
Related
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
I'd like to pass an entire (custom) class reference to another activity.
This class is called WeekProgramData. WeekProgramData has an array of 7 Day (class) instances and each Day class has several Switch instances.
How can I pass along the class reference between activities, so I can use the methods from that class etc. in other activities?
I already tried the following, but it failed: First, declaration in Activity A
WeekProgramData wpd = new WeekProgramData();
Code for passing the WeekProgramData class reference in activity A:
Intent intent = new Intent(v.getContext(), WeekOverview.class);
intent.putExtra("wpd", wpd);
startActivity(intent);
Code in Activity B for getting the class reference:
Bundle extras = getIntent().getExtras();
WeekProgramData wpd = extras.getWeekProgramData("wpd");
To pass a class via intent you have to implement the Parcelable interface. Here is a tutorial on how to do it and it shouldn't take you too long. Once you do this you can pass instances of that class via an intent to whatever you want.
The way to pass custom classes between activities is to make your classes Parcelable. This is a way of serializing your data so it can easily be sent around. Once your classes are Parcelable you can use the standard putExtra and pass your class in. To retrieve your class you can use getParcelableExtra. With Parcelable remember you cannot parcel null values and all nested classes must be parcelable as well. In your case you will have to make your WeekProgramData class parcelable as well as your Day and Switch classes.
There could a couple of diiferent ways to do this, but the two best options in my opinion are:
WeekProgramData should implement Parcelable
or convert wpd into String using Gson, pass that string to your second activity and finally reconstruct your object from that string using Gson again
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?
I am implementing the DataDroid model of RESTful communication and I have come across a compiler error while implementing my SearchCriteria. The problem is that in order to pass the SearchCriteria around as an intent extra, I had to make it implement Parcelable. However, my Worker's start function requires a SearchCriteria class, leading to the error: Required: my.classes.SearchCriteria; Found: android.os.Parcelable.
Assuming that I've correctly implemented Parcelable for my SearchCriteria class, how can I quickly create an object from a parcel (where the parcel is found using getParcelable Extra(INTENT_NAME)?
Edit: I realize I can accomplish this quickly by making my constructor for SearchCriteria from Parcel public, but is there another way? Actually, this does not work - I confused a Parcel with a Parcelable thing.
Suppose you follow the API and make SearchCriteria implements Parcelable properly, and your SearchCriteria has been properly constructed or instantiated from the underlying business layer, either from a Database or a Http server or etc.
To pass it to next activity by intent:
SearchCriteria searchCriteria = createSearchCriteria();
Intent intent = new Intent(getBaseContext(), NextActivity.class);
intent.putExtra("searchCriteria", searchCriteria);
startActivity(intent);
To retrieve it from intent in next activity:
SearchCriteria searchCriteria = getIntent().getParcelableExtra("searchCriteria");
myWorker.search(searchCriteria);
In most situation, we don't need bother Parcel directly.
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