Syntax for creating a Object implementing Parcelable from getParcelableExtra(intent_name) - android

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.

Related

Passing entire class reference via Intent

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

Sending objects between activities

There are two activity classes in my project and a third class which is subclass of Thread.
Thread Class implements Bluetooth Socket which isn't Parcelable.
First Activity starts the Second Activity using startActivityforResult()
Second Activity creates an object of the Thread class and starts the thread.
I need to pass an object reference of the Thread object from Second Activity to First Activity's onActivityResult() so that I can access Thread object from the first activity.
How can I achieve this?
You have a few options.
You can either break down your object into simple data types and put those values as extras on the intent that you pass back with setResult(), do do so you'd use intent.putExtra(key, value)
Or you can make your data object implement the Parcelable interface so that you can add the data object directly to the intent.
the code to do the latter would look something like this
Intent resultIntent = new Intent();
resultIntent.putExtra("resultObject", mObj);
setResult(ACTION_OK, resultIntent);
then inside your onActivityResult you can pull it out like this:
data.getParcelableExtra("resultObject");
For the latter method to work you need to correctly implement parcelable with your data object. The former method does not require this however, since you'll be passing back simple values only. You'd then have to take those simple values and "re-inflate" the data object on the other side.
I think the best way to achieve this would be to use a singleton. You can only store primitives in Shared Preferences and Bundles. Here is a great reference for creating a singleton.
http://www.javaworld.com/javaworld/jw-04-2003/jw-0425-designpatterns.html

Why objects must be serialized before being passed to activities in android?

In order to pass an object to an Activity, object must implement Parcelable/Serializable (or JSON-encoded, or whatever casts the object to a scalar).
(I know the alternative of using singletons, statics and so on.)
Why it is not possible to give a POJO to my Activity, something like new Intent( ... , myObject) or startActivity(intent, myObject) ?
I turned #Jens's comment into a community wiki:
Your call to startActivity is passed through to ActivityManagerNative - which, in turn, communicates with the activity manager service that runs in a completely different process - thus forcing whatever you put in your Intent to be Parcelable or Serializable

Android - Problem with the Serializable interface

I have been using the Serializable interface to pass an object from one activity to another. I am using putExtra on the sender side and getSerializable on the receiver side. Everything works fine but I have received (for the first time) the following error report:
java.lang.RuntimeException: Parcelable encountered IOException reading
a Serializable object
I don't understand why this exception has been generated since I am using getSerializable and not getParcelable.
I know that I should implement the Parcelable interface instead because it has been designed specifically for Android (and that's what I will end up doing) but I want to understand why I am getting this error.
Thanks!
Parcelable is mentioned in this error because an Intent you send from one Activity to another has a Bundle inside and this Bundle is Parcelable. When you call Intent.putExtra() this extra is added to the inner Bundle. When Intent is passed between activities its Bundle is converted to and from a byte array and so is your Serializable object.
But I don't know why this error occurs. Maybe it's because of some bug in writeObject()/readObject() implementation.

Android: Serializable Intent

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.

Categories

Resources